Android buttons: Four event types
In the course of studying Android, whether watching videos or blogs, I found that everyone has different preferences in coding, obviously, it is different in writing controls to respond to events. So I want to summarize these writing methods and compare the advantages and disadvantages of different writing methods. I hope that I can flexibly select the encoding method.
Xml file
1 <Button 2 android:id="@+id/button1" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:text="Button1" /> 6 7 <Button 8 android:id="@+id/button2" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:text="Button2" />
Four methods:
1 public class TestButtonActivity extends Activity { 2 3 Button btn1, btn2; 4 Toast tst; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_test_button);10 11 btn1 = (Button) findViewById(R.id.button1);12 btn2 = (Button) findViewById(R.id.button2);13 14 btn1.setOnClickListener(new OnClickListener() {15 16 @Override17 public void onClick(View v) {18 // TODO Auto-generated method stub19 Toast tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT);20 tst.show();21 22 }23 });24 25 btn2.setOnClickListener(new OnClickListener() {26 27 @Override28 public void onClick(View v) {29 // TODO Auto-generated method stub30 Toast tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT);31 tst.show();32 }33 });34 }35 36 }
- Custom click event listening class
1 public class TestButtonActivity extends Activity { 2 3 Button btn1, btn2; 4 Toast tst; 5 6 class MyClickListener implements OnClickListener { 7 8 @Override 9 public void onClick(View v) {10 // TODO Auto-generated method stub11 switch (v.getId()) {12 case R.id.button1:13 tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT);14 tst.show();15 break;16 case R.id.button2:17 tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT);18 tst.show();19 break;20 default:21 break;22 }23 }24 25 }26 27 @Override28 protected void onCreate(Bundle savedInstanceState) {29 super.onCreate(savedInstanceState);30 setContentView(R.layout.activity_test_button);31 32 btn1 = (Button) findViewById(R.id.button1);33 btn2 = (Button) findViewById(R.id.button2);34 35 btn1.setOnClickListener(new MyClickListener());36 btn2.setOnClickListener(new MyClickListener());37 }38 39 }
- The 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.
1 public class TestButtonActivity extends Activity implements OnClickListener { 2 3 Button btn1, btn2; 4 Toast tst; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_test_button);10 11 btn1 = (Button) findViewById(R.id.button1);12 btn2 = (Button) findViewById(R.id.button2);13 14 btn1.setOnClickListener(this);15 btn2.setOnClickListener(this);16 }17 18 @Override19 public void onClick(View v) {20 // TODO Auto-generated method stub21 switch (v.getId()) {22 case R.id.button1:23 tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT);24 tst.show();25 break;26 case R.id.button2:27 tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT);28 tst.show();29 break;30 default:31 break;32 }33 }34 }
- The last one is a method I saw today. In the XML file, "The onClick attribute of the specified button is displayed. In this way, when you click a button, the click () in the corresponding Activity is called Using Reflection () method "[NOTE 1]
1 <Button 2 android:id="@+id/button1" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:onClick="onClick" 6 android:text="Button1" /> 7 8 <Button 9 android:id="@+id/button2"10 android:layout_width="wrap_content"11 android:layout_height="wrap_content"12 android:onClick="onClick"13 android:text="Button2" />
Here, the message indicating the onClick attribute is displayed when you press Alt +/when you enter android: onClick = ". However, when you press Alt +/, The onClick option is not displayed, it suddenly seems a problem to me.
1 public class TestButtonActivity extends Activity {2 3 Button btn1, btn2; 4 Toast tst; 5 6 @ Override 7 protected void onCreate (Bundle savedInstanceState) {8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. activity_test_button); 10} 11 12 // note that there is no @ Override label 13 public void onClick (View v) {14 // TODO Auto-generated method stub15 switch (v. getId () {16 case R. id. button1: 17 tst = Toast. makeText (this, "111111111", Toast. LENGTH_SHORT); 18 tst. show (); 19 break; 20 case R. id. button2: 21 tst = Toast. makeText (this, "222222222", Toast. LENGTH_SHORT); 22 tst. show (); 23 break; 24 default: 25 break; 26} 27} 28}
In this way, the Click Event of the button can be implemented without declaring the button in the code.
The above are four methods for implementing button-click events.
In a rough summary, it is faster to use an anonymous internal class when there are few buttons, such as writing a demo test or logging on to the interface.
When there are many buttons, I chose the third method for convenience.
I feel the most convenient about the fourth method, but I still think that the writing is quite small after reading a lot of code. I still need to study this point for some reason.
[NOTE 1] Four methods for responding to click events from Android-Button
What he said
1 public void click(View v){2 ...3 }
I tried the method and an error will be reported during the running. We recommend that you use the sample code above. The time was too short to take a closer look at the cause of the error.
In android, add a click event to you.
Public onCreate (Bundle bundle ){
Button btn = (Button) findViewById (R. id. button1 );
Btn. setOnClickListener (listener );
}
Public onClickListener listener = new onClickListener (this ){
Public void onClick (View paramView ){
// What you want to do
} |
}
Android button add Click Event
First, the number has an initial value. Suppose int num = 0;
Bt. setOnClickListener (new OnClickListener (){
Public void onClick {
Num ++;
TV. setText (num );
}
});