Android开发之Button事件实现与监听方法总结
程序员文章站
2023-12-18 15:03:46
本文实例总结了android开发之button事件实现与监听方法。分享给大家供大家参考,具体如下:
先来介绍button事件实现的两种方法
main.xml...
本文实例总结了android开发之button事件实现与监听方法。分享给大家供大家参考,具体如下:
先来介绍button事件实现的两种方法
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 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:id="@+id/mybutton1" android:text=" 按钮1 " android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/mybutton2" android:text=" 按钮2 " android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">hello world, buttondemoactivity!</string> <string name="app_name">buttondemo</string> </resources>
第一种:
buttondemoactivity.java:
package com.android.buttondemo.activity; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; public class buttondemoactivity extends activity { button mybutton1,mybutton2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mybutton1=(button)findviewbyid(r.id.mybutton1); mybutton2=(button)findviewbyid(r.id.mybutton2); //使用匿名类注册button事件 mybutton1.setonclicklistener(new onclicklistener() { public void onclick(view v) { toast.maketext(buttondemoactivity.this, "你点击了按钮1",toast.length_long).show(); } }); mybutton2.setonclicklistener(new onclicklistener() { public void onclick(view v) { toast.maketext(buttondemoactivity.this, "你点击了按钮2",toast.length_long).show(); } }); } }
第二种:
buttondemoactivity.java:
package com.android.buttondemo.activity; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; public class buttondemoactivity extends activity { button mybutton1,mybutton2; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mybutton1=(button)findviewbyid(r.id.mybutton1); mybutton2=(button)findviewbyid(r.id.mybutton2); mybutton1.setonclicklistener(new buttonclick()); mybutton2.setonclicklistener(new buttonclick()); } //创建一个类,来响应onclicklistener class buttonclick implements onclicklistener { public void onclick(view v) { switch (v.getid()) { case r.id.mybutton1: toast.maketext(buttondemoactivity.this, "你点击了按钮1",toast.length_long).show(); break; case r.id.mybutton2: toast.maketext(buttondemoactivity.this, "你点击了按钮2",toast.length_long).show(); break; default: break; } } } }
再来说说button监听方法
android button控件目前主要有如下几种监听方式:
一个button控件对应一个监听:
button buttontest; buttontest = (button) findviewbyid(r.id.button1); buttontest.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub log.i("test", "button onclick"); } });
多个button对应一个监听1:
start = (button) findviewbyid(r.id.button1); stop = (button) findviewbyid(r.id.button2); start.setonclicklistener(mylistener ); stop.setonclicklistener(mylistener ); view.onclicklistener mylistener = new view.onclicklistener() { @override public void onclick(view v) { switch (v.getid()) { case r.id.button1: log.d(tag, "start to recorder video\n"); start_recorde(); break; case r.id.button2: log.d(tag, "stop to recorder video\n"); stop_recorde(); break; default: break; } } };
多个button对应一个监听2:
public class mainactivity extends activity implements view.onclicklistener{ //界面元素 private button start; private button stop; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); start = (button) findviewbyid(r.id.button1); stop = (button) findviewbyid(r.id.button2); start.setonclicklistener(this); stop.setonclicklistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main, menu); return true; } @override public void onclick(view v) { switch (v.getid()) { case r.id.button1: log.d(tag, "start to recorder video\n"); break; case r.id.button2: log.d(tag, "stop to recorder video\n"); break; default: break; } } }
xml中绑定监听:
<button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="mybuttonlistener"> </button>
对应java代码如下:
button btn = (button) findviewbyid(r.id.button1); public void mybuttonlistener(view target){ //do something5 }
从个人角度来讲,不推荐使用第四种实现方式。
希望本文所述对大家android程序设计有所帮助。