本文实例总结了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程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。