Android中button的onClick事件几种方法
程序员文章站
2023-12-19 19:16:04
android中button的onclick事件几种方法
利用三种方法,学习button的监听事件。
方法一源码如下:
package com.example...
android中button的onclick事件几种方法
利用三种方法,学习button的监听事件。
方法一源码如下:
package com.example.androidtest; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.widget.button; import android.widget.edittext; import android.widget.toast; import android.view.view; import android.widget.textview; import android.view.view.onclicklistener; import android.widget.button; //import android.text.editable; //import android.text.textwatcher; import android.widget.edittext; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button button1 = (button)findviewbyid(r.id.button1); button button2 = (button)findviewbyid(r.id.button2); //方法一: //增加事件响应 button1.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { intent intent =new intent(); intent.setclass(mainactivity.this, testactivity.class); mainactivity.this.startactivity(intent); } }); button2.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { intent intent =new intent(); intent.setclass(mainactivity.this, tactivity.class); mainactivity.this.startactivity(intent); } }); }
方法二:
package com.example.androidtest; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.widget.button; import android.widget.edittext; import android.widget.toast; import android.view.view; import android.widget.textview; import android.view.view.onclicklistener; import android.widget.button; //import android.text.editable; //import android.text.textwatcher; import android.widget.edittext; public class mainactivity extends activity { @override { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button button1 = (button)findviewbyid(r.id.button1); button button2 = (button)findviewbyid(r.id.button2); } //方法二: public void myclickhandler(view target){ switch (target.getid()) { case r.id.button1: intent music=new intent(); music.setclass(mainactivity.this, testactivity.class); mainactivity.this.startactivity(music); // settitle("myclickhandler01"); break; case r.id.button2: // settitle("myclickhandler02"); intent moive=new intent(); moive.setclass(mainactivity.this, tactivity.class); mainactivity.this.startactivity(moive); break; } } }
方法三:
package com.example.androidtest; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.widget.button; import android.widget.edittext; import android.widget.toast; import android.view.view; import android.widget.textview; import android.view.view.onclicklistener; import android.widget.button; //import android.text.editable; //import android.text.textwatcher; import android.widget.edittext; public class mainactivity extends activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } //方法三: public void myclickhandler01(view target){ //设置activity的头标题 // settitle("myclickhandler01"); intent music=new intent(); music.setclass(mainactivity.this, testactivity.class); mainactivity.this.startactivity(music); } public void myclickhandler02(view target){ //设置activity的头标题 // settitle("myclickhandler02"); intent moive=new intent(); moive.setclass(mainactivity.this, tactivity.class); mainactivity.this.startactivity(moive); } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!