Several methods of onClick event of button in Android
Use three methods to learn button listening events.
Method 1 source code is as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; //import ; //import ; import ; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); Button button1 = (Button)findViewById(.button1); Button button2 = (Button)findViewById(.button2); //Method 1: //Increase incident response (new () { public void onClick(View v) { Intent intent =new Intent(); (, ); (intent); } }); (new () { public void onClick(View v) { Intent intent =new Intent(); (, ); (intent); } }); }
Method 2:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; //import ; //import ; import ; public class MainActivity extends Activity { @Override { (savedInstanceState); setContentView(.activity_main); Button button1 = (Button)findViewById(.button1); Button button2 = (Button)findViewById(.button2); } //Method 2: public void myClickHandler(View target){ switch (()) { case .button1: Intent music=new Intent(); (, ); (music); // setTitle("myClickHandler01"); break; case .button2: // setTitle("myClickHandler02"); Intent moive=new Intent(); (, ); (moive); break; } } }
Method 3:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; //import ; //import ; import ; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); } //Method 3: public void myClickHandler01(View target){ //Set the header of the activity // setTitle("myClickHandler01"); Intent music=new Intent(); (, ); (music); } public void myClickHandler02(View target){ //Set the header of the activity // setTitle("myClickHandler02"); Intent moive=new Intent(); (, ); (moive); } }
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!