android - One listener - Multiple buttons -



android - One listener - Multiple buttons -

i'm trying create calculator on eclipse android application. i'm having problem listener multiple buttons. know many solutions available on net project i'm learning understand mistake.

here xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.rayana.calculator.mainactivity" > <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/button1" android:layout_margintop="39dp" android:text="result =" android:textappearance="?android:attr/textappearancelarge" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_alignleft="@+id/button1" android:layout_marginbottom="31dp" android:text="b =" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/edittext1" android:layout_alignleft="@+id/textview2" android:text="a =" /> <edittext android:id="@+id/edittext1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/edittext2" android:layout_alignleft="@+id/edittext2" android:layout_marginbottom="45dp" android:ems="10" android:inputtype="numberdecimal" /> <edittext android:id="@+id/edittext2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textview2" android:layout_alignbottom="@+id/textview2" android:layout_alignleft="@+id/button2" android:ems="10" android:inputtype="numberdecimal" /> <button android:id="@+id/button2" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textview3" android:layout_torightof="@+id/textview3" android:text="-" /> <button android:id="@+id/button1" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/textview3" android:layout_centervertical="true" android:layout_marginright="16dp" android:text="+" /> <button android:id="@+id/button3" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview2" android:layout_marginleft="16dp" android:layout_torightof="@+id/button2" android:text="x" /> <button android:id="@+id/button4" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textview2" android:layout_marginleft="16dp" android:layout_torightof="@+id/button3" android:text="÷" /> <edittext android:id="@+id/edittext3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_aligntop="@+id/textview3" android:ems="10" android:inputtype="numberdecimal" /> </relativelayout>

and here main.java:

import android.support.v7.app.actionbaractivity; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.os.bundle; public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button plus = (button)findviewbyid(r.id.button1); button minus = (button)findviewbyid(r.id.button2); button multiply = (button)findviewbyid(r.id.button3); button devise = (button)findviewbyid(r.id.button4); final edittext edittext1 = (edittext)findviewbyid(r.id.edittext1); final double = double.valueof(edittext1.gettext().tostring()); final edittext edittext2 = (edittext)findviewbyid(r.id.edittext2); final double b = double.valueof(edittext2.gettext().tostring()); final edittext edittext3 = (edittext)findviewbyid(r.id.edittext3); plus.setonclicklistener(operation1); minus.setonclicklistener(operation1); multiply.setonclicklistener(operation1); devise.setonclicklistener(operation1); onclicklistener operation1 = new onclicklistener() { public void onclick(view v) { switch(v.getid()){ case r.id.button1: double result = a+b; edittext3.settext(string.valueof(result)); break; case r.id.button2: double result2 = a-b; edittext3.settext(string.valueof(result2)); break; case r.id.button3: double result3 = a*b; edittext3.settext(string.valueof(result3)); break; case r.id.button4: double result4 = a/b; edittext3.settext(string.valueof(result4)); break; } } }; }

write blow code outside oncreate() method.

public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button plus = (button)findviewbyid(r.id.button1); button minus = (button)findviewbyid(r.id.button2); button multiply = (button)findviewbyid(r.id.button3); button devise = (button)findviewbyid(r.id.button4); final edittext edittext1 = (edittext)findviewbyid(r.id.edittext1); final double = double.valueof(edittext1.gettext().tostring()); final edittext edittext2 = (edittext)findviewbyid(r.id.edittext2); final double b = double.valueof(edittext2.gettext().tostring()); final edittext edittext3 = (edittext)findviewbyid(r.id.edittext3); plus.setonclicklistener(operation1); minus.setonclicklistener(operation1); multiply.setonclicklistener(operation1); devise.setonclicklistener(operation1); } ///your click listener method: onclicklistener operation1 = new onclicklistener() { public void onclick(view v) { switch(v.getid()){ case r.id.button1: double result = a+b; edittext3.settext(string.valueof(result)); break; case r.id.button2: double result2 = a-b; edittext3.settext(string.valueof(result2)); break; case r.id.button3: double result3 = a*b; edittext3.settext(string.valueof(result3)); break; case r.id.button4: double result4 = a/b; edittext3.settext(string.valueof(result4)); break; } } }; }

android calculator listeners

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -