This article summarizes the Button event implementation and listening methods for Android development. Share it for your reference, as follows:
Let's first introduce two methods of implementing Button events
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android: android:text="Button 1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android: android:text="Button 2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ButtonDemoActivity!</string> <string name="app_name">ButtonDemo</string> </resources>
The first type:
:
package ; import ; import ; import ; import ; import ; import ; public class ButtonDemoActivity extends Activity { Button myButton1,myButton2; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); myButton1=(Button)findViewById(.myButton1); myButton2=(Button)findViewById(.myButton2); //Register Button events using anonymous class (new OnClickListener() { public void onClick(View v) { (, "You clicked button 1",Toast.LENGTH_LONG).show(); } }); (new OnClickListener() { public void onClick(View v) { (, "You clicked the button 2",Toast.LENGTH_LONG).show(); } }); } }
The second type:
:
package ; import ; import ; import ; import ; import ; import ; public class ButtonDemoActivity extends Activity { Button myButton1,myButton2; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); myButton1=(Button)findViewById(.myButton1); myButton2=(Button)findViewById(.myButton2); (new ButtonClick()); (new ButtonClick()); } //Create a class to respond to OnClickListener class ButtonClick implements OnClickListener { public void onClick(View v) { switch (()) { case .myButton1: (, "You clicked button 1",Toast.LENGTH_LONG).show(); break; case .myButton2: (, "You clicked the button 2",Toast.LENGTH_LONG).show(); break; default: break; } } } }
Let's talk about Button monitoring method
There are currently the following listening methods for Android button control:
A button control corresponds to a listener:
Button buttontest; buttontest = (Button) findViewById(.button1); (new () { @Override public void onClick(View v) { // TODO Auto-generated method stub ("TEST", "button onClick"); } });
Multiple buttons correspond to one listener:
start = (Button) findViewById(.button1); stop = (Button) findViewById(.button2); (mylistener ); (mylistener ); mylistener = new () { @Override public void onClick(View v) { switch (()) { case .button1: (TAG, "Start to recorder video\n"); start_recorde(); break; case .button2: (TAG, "Stop to recorder video\n"); stop_recorde(); break; default: break; } } };
Multiple buttons correspond to one listener 2:
public class MainActivity extends Activity implements { //Interface elements private Button start; private Button stop; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); start = (Button) findViewById(.button1); stop = (Button) findViewById(.button2); (this); (this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(.activity_main, menu); return true; } @Override public void onClick(View v) { switch (()) { case .button1: (TAG, "Start to recorder video\n"); break; case .button2: (TAG, "Stop to recorder video\n"); break; default: break; } } }
Binding listener in xml:
<Button android: android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="mybuttonlistener"> </Button>
The corresponding java code is as follows:
Button btn = (Button) findViewById(.button1); public void mybuttonlistener(View target){ //do something5 }
From a personal point of view, the fourth implementation method is not recommended.
I hope this article will be helpful to everyone's Android programming design.