Tips for how to implement your own click events in the same listener for multiple buttons.

Source: Internet
Author: User

In some of the online tutorials are often a button to add multiple listeners, but few people will put multiple buttons in the same listener to achieve their click events, and this is actually very practical, such as in the production of a simple calculator is needed 0-9, These 10 number buttons if you want to write the click events of these 10 buttons individually, you can imagine that the code is definitely not robust enough, and such a program will also affect the speed of its operation. Such programs are easy to implement in Java, but there is a lot of knowledge involved in Android, so let's take a look at the main code:

Use Windows as listeners.

public class Index extends Activity implements onclicklistener{

Button Bs0,bs1,bs2,bs3,bs4,bs5,bs6,bs7,bs8,bs9,call,c;

EditText edit;

Button del;

private static final int count=0;

public int Bi[]={r.id.i0,r.id.i1,r.id.i2,r.id.i3,r.id.i4,r.id.i5,r.id.i6,r.id.i7,r.id.i8,r.id.i9};

bs0= (Button) Findviewbyid (bi[0]);

bs1= (Button) Findviewbyid (bi[1]);

Bs2= (Button) Findviewbyid (bi[2]);

bs3= (Button) Findviewbyid (bi[3]);

bs4= (Button) Findviewbyid (bi[4]);

bs5= (Button) Findviewbyid (bi[5]);

bs6= (Button) Findviewbyid (bi[6]);

bs7= (Button) Findviewbyid (bi[7]);

bs8= (Button) Findviewbyid (bi[8]);

bs9= (Button) Findviewbyid (bi[9]);

edit= (EditText) Findviewbyid (R.id.edit);

Call= (Button) Findviewbyid (R.id.call);

C= (Button) Findviewbyid (R.ID.C);

Del= (Button) Findviewbyid (R.id.del);

Bs0.setonclicklistener (this);

Bs1.setonclicklistener (this);

Bs2.setonclicklistener (this);

Bs3.setonclicklistener (this);

Bs4.setonclicklistener (this);

Bs5.setonclicklistener (this);

Bs6.setonclicklistener (this);

Bs7.setonclicklistener (this);

Bs8.setonclicklistener (this);

Bs9.setonclicklistener (this);

Call.setonclicklistener (this);

C.setonclicklistener (this);

Del.setonclicklistener (this);

}

Overriding the OnClick () method

public void OnClick (View v) {

Use HashMap to treat a button as a key to value a number.

Integer i0=0,i1=1,i2=2,i3=3,i4=4,i5=5,i6=6,i7=7,i8=8,i9=9,d=10,ca=11,c1=12;

HashMapbf=new HashMap();

Bf.put (Bs0, I0); Bf.put (BS1, I1); Bf.put (BS2, I2); Bf.put (BS3, i3); Bf.put (BS4, I4); Bf.put (BS5, i5); Bf.put (BS6, I6);

Bf.put (BS7, i7); Bf.put (BS8, i8); Bf.put (Bs9, I9); Bf.put (Del, D); Bf.put (call, CA); Bf.put (c, C1);

Button bbs= (button) v;

int Ii=bf.get (v);

if (ii<10) {

String s1=string.valueof (ii);

Edit.append (S1);

}

You add their click events to different buttons or other controls, depending on the values of the obtained numbers.

else if (ii==10) {

String S3=edit.gettext (). toString ();

System.out.print (S3);

S3= (S3.length () >1) s3.substring (0,s3.length ()-1): "";

Edit.settext (S3);

}

else if (ii==11) {

}

else if (ii==12) {

Index.this.finish ();

}

}

Tips for how to implement your own click events in the same listener for multiple buttons.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.