Android开发之Button事件实现与监听方法总结
作者:零基础 时间:2022-02-05 02:45:40
本文实例总结了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程序设计有所帮助。
标签:Android,Button
0
投稿
猜你喜欢
SpringBoot返回Json对象报错(返回对象为空{})
2022-06-30 03:31:19
Java如何基于ProcessBuilder类调用外部程序
2023-11-27 20:19:57
Java 深入浅出掌握Collection单列集合Set
2023-10-07 18:49:32
C#使用FolderBrowserDialog类实现选择打开文件夹方法详解
2022-08-11 07:28:39
Java中的同步与异步详细介绍
2023-06-23 00:42:08
SpringBoot雪花算法主键ID传到前端后精度丢失问题的解决
2022-07-18 02:30:47
使用maven开发springboot项目时pom.xml常用配置(推荐)
2022-09-19 23:33:48
简单了解redis常见客户端及Sharding机制原理
2022-03-28 16:49:09
C#调用JS的几种方法
2022-09-29 23:24:18
json格式数据分析工具PageElement类分享(仿Session写法)
2023-09-16 16:04:32
Java Cookie与Session实现会话跟踪详解
2022-12-22 08:59:13
SpringBoot整合Mybatis与thymleft实现增删改查功能详解
2023-01-06 00:22:59
android实现歌词自动滚动效果
2022-03-16 17:09:10
解析C#中的常量及如何在C#编程中定义常量
2022-03-20 22:58:36
java 利用反射获取内部类静态成员变量的值操作
2023-03-28 21:30:04
Flutter封装组动画混合动画AnimatedGroup示例详解
2022-12-29 17:17:05
Spring配置文件如何使用${username}
2021-07-26 22:40:27
C#给PDF文件添加水印
2023-11-10 17:08:36
C#实现飞行棋小游戏
2022-10-28 22:00:02
Java实现通讯录管理系统项目
2022-11-17 02:41:03