Android中button的onClick事件几种方法
Android中button的onClick事件几种方法
利用三种方法,学习button的监听事件。
方法一源码如下:
packagecom.example.androidtest; importandroid.os.Bundle; importandroid.app.Activity; importandroid.content.Intent; importandroid.view.Menu; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.Toast; importandroid.view.View; importandroid.widget.TextView; importandroid.view.View.OnClickListener; importandroid.widget.Button; //importandroid.text.Editable; //importandroid.text.TextWatcher; importandroid.widget.EditText; publicclassMainActivityextendsActivity { @Override protectedvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Buttonbutton1=(Button)findViewById(R.id.button1); Buttonbutton2=(Button)findViewById(R.id.button2); //方法一: //增加事件响应 button1.setOnClickListener(newButton.OnClickListener() { publicvoidonClick(Viewv) { Intentintent=newIntent(); intent.setClass(MainActivity.this,TestActivity.class); MainActivity.this.startActivity(intent); } }); button2.setOnClickListener(newButton.OnClickListener() { publicvoidonClick(Viewv) { Intentintent=newIntent(); intent.setClass(MainActivity.this,TActivity.class); MainActivity.this.startActivity(intent); } }); }
方法二:
packagecom.example.androidtest; importandroid.os.Bundle; importandroid.app.Activity; importandroid.content.Intent; importandroid.view.Menu; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.Toast; importandroid.view.View; importandroid.widget.TextView; importandroid.view.View.OnClickListener; importandroid.widget.Button; //importandroid.text.Editable; //importandroid.text.TextWatcher; importandroid.widget.EditText; publicclassMainActivityextendsActivity { @Override { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Buttonbutton1=(Button)findViewById(R.id.button1); Buttonbutton2=(Button)findViewById(R.id.button2); } //方法二: publicvoidmyClickHandler(Viewtarget){ switch(target.getId()){ caseR.id.button1: Intentmusic=newIntent(); music.setClass(MainActivity.this,TestActivity.class); MainActivity.this.startActivity(music); //setTitle("myClickHandler01"); break; caseR.id.button2: //setTitle("myClickHandler02"); Intentmoive=newIntent(); moive.setClass(MainActivity.this,TActivity.class); MainActivity.this.startActivity(moive); break; } } }
方法三:
packagecom.example.androidtest; importandroid.os.Bundle; importandroid.app.Activity; importandroid.content.Intent; importandroid.view.Menu; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.Toast; importandroid.view.View; importandroid.widget.TextView; importandroid.view.View.OnClickListener; importandroid.widget.Button; //importandroid.text.Editable; //importandroid.text.TextWatcher; importandroid.widget.EditText; publicclassMainActivityextendsActivity { protectedvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //方法三: publicvoidmyClickHandler01(Viewtarget){ //设置activity的头标题 //setTitle("myClickHandler01"); Intentmusic=newIntent(); music.setClass(MainActivity.this,TestActivity.class); MainActivity.this.startActivity(music); } publicvoidmyClickHandler02(Viewtarget){ //设置activity的头标题 //setTitle("myClickHandler02"); Intentmoive=newIntent(); moive.setClass(MainActivity.this,TActivity.class); MainActivity.this.startActivity(moive); } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!