Android button Common Click event Big Summary

Source: Internet
Author: User
Tags event listener

Many people who study Android programming find that each person has different preferences for how the code is written, and what is more obvious is the difference in how the control responds to events. Therefore this article summarizes these writing, compares each kind of writing the merits and demerits, hoped that everybody flexibly chooses the code way may have the reference value.

The XML file code 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" />The four methods are described as follows: 1. Anonymous inner class: 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() {Or   btn1.setOnClickListener( newView.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() {Or   btn2.setOnClickListener( newView.onclicklistener () {        @Override        public void onClick(View v) {          // TODO Auto-generated method stub          Toast tst = Toast.makeText(TestButtonActivity. this , "222222222" , Toast.LENGTH_SHORT);          tst.show();        }      });    } }2. Custom Click event Listener class: 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());    } }Or first define a Onclicklistener object, and implement its onclick (View) method ... Onclicklistener listener = new Onclicklistener () { //  @Override //     public void onClick(View v) {/* Write a button here to execute the code *///};//bt.setonclicklistener (listener); 3. This is a notation that I see today, in the XML file, "displays the OnClick property of the specified button, This allows you to invoke the click () method in the corresponding activity using reflection when the button is clicked. < Button      android:id = "@+id/button1"      android:layout_width = "wrap_content"      android:layout_height = "wrap_content"      android:onClick = "onClick"      android:text = "Button1" />      < Button      android:id = "@+id/button2"      android:layout_width = "wrap_content"      android:layout_height = "wrap_content"      android:onClick = "onClick"      android:text = "Button2" />Here in the end of the android: Press alt+/will have the OnClick property prompt, but the input to android:onclick= "place pressed alt+/and did not prompt the OnClick option, let me suddenly feel there seems to be a problem. 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);    }      // 注意 这里没有 @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 ;      }    } }In this case, the button's Click event can be implemented without declaring the button in the entire code. 4. Similar to the previous approach, but a little more specific

General monitoring Onclicklistener Events, we are all through button button = (button) Findviewbyid (...);

Button.setoclicklisener .... Such a way to achieve.

This period of time to see the major open platform of the demo, found in the XML can actually define a good method name, in the activity to implement the method can be.

<button

Android:layout_width= "70DP"

android:layout_height= "Wrap_content"

Android:layout_centervertical= "true"

android:background= "@drawable/title_btn_back"

android:onclick= "AA"

Android:text= "Back"

Android:textcolor= "#fff"

Android:textsize= "14sp"/>

public void AA (View v) {

/* Write the code that executes when the button is clicked in here */

}

This method can be implemented directly in the activity, and there is no need to define a button to instantiate it, which is much more convenient than the original one.

Android button Common Click event Big Summary

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.