Android button click event four common writing methods, android four

Source: Internet
Author: User

Android button click event four common writing methods, android four

The xml layout is as follows:

<Button    android:id="@+id/button1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Button1" />   <Button    android:id="@+id/button2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Button2" />

First: anonymous internal class

Applicable scenarios: ---------------------------------------------------------- test or single button

public class TestButtonActivity extends Activity {   Button btn1, btn2;  Toast tst;   @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_test_button);     btn1 = (Button) findViewById(R.id.button1);    btn2 = (Button) findViewById(R.id.button2);     btn1.setOnClickListener(new OnClickListener() {       @Override      public void onClick(View v) {        // TODO Auto-generated method stub        Toast tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT);        tst.show();       }    });     btn2.setOnClickListener(new OnClickListener() {       @Override      public void onClick(View v) {        // TODO Auto-generated method stub        Toast tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT);        tst.show();      }    });  }}

The second type of custom click event listening class:

Rarely used!


public class TestButtonActivity extends Activity {   Button btn1, btn2;  Toast tst;   class MyClickListener implements OnClickListener {     @Override    public void onClick(View v) {      // TODO Auto-generated method stub      switch (v.getId()) {      case R.id.button1:        tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT);        tst.show();        break;      case R.id.button2:        tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT);        tst.show();        break;      default:        break;      }    }  }   @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_test_button);     btn1 = (Button) findViewById(R.id.button1);    btn2 = (Button) findViewById(R.id.button2);     btn1.setOnClickListener(new MyClickListener());    btn2.setOnClickListener(new MyClickListener());  }}
The third type of Activity inherits View. OnClickListener, and The OnClick (View view) method is implemented by the Activity. In The OnClick (View view) method, switch-case is used to process the buttons represented by different IDs.

~~~~~~~~~~~~~~~ · It is often used. It is generally used when there are many buttons ~~~~~~~~~~ ·····

public class TestButtonActivity extends Activity implements OnClickListener {   Button btn1, btn2;  Toast tst;   @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_test_button);     btn1 = (Button) findViewById(R.id.button1);    btn2 = (Button) findViewById(R.id.button2);     btn1.setOnClickListener(this);    btn2.setOnClickListener(this);  }   @Override  public void onClick(View v) {    // TODO Auto-generated method stub    switch (v.getId()) {    case R.id.button1:      tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT);      tst.show();      break;    case R.id.button2:      tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT);      tst.show();      break;    default:      break;    }  }}

Fourth, you do not need to set the Click Event in The Onclick Event code.

Public class TestButtonActivity extends Activity {Button btn1, btn2; Toast tst; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_test_button);} // note that there is no @ Override label public void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {case R. id. button1: tst = Toast. makeText (this, "111111111", Toast. LENGTH_SHORT); tst. show (); break; case R. id. button2: tst = Toast. makeText (this, "222222222", Toast. LENGTH_SHORT); tst. show (); break; default: break ;}}}



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.