一个监听器(Listener)实现多个按钮的onclick监听
2015-09-16 19:22
489 查看
在android应用程序中,有时要用到很多的按钮元件,每个按钮都要有一个监听事件,为了让代码看起来干净简洁,并节省一些内存,我们可以用一个监听器(Listener)来实现多个按钮的onClick监听,下面是一个具体的例子:
[java] view
plaincopy
package com.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentSelectActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.btn1);
Button button2 = (Button)findViewById(R.id.btn2);
Button button3 = (Button)findViewById(R.id.btn3);
button1.setOnClickListener(this);
button1.setTag(1);
button2.setOnClickListener(this);
button2.setTag(2);
button3.setOnClickListener(this);
button3.setTag(3);
}
public void onClick(View v){
int tag = (Integer) v.getTag();
switch(tag){
case 1:
Intent music = new Intent(Intent.ACTION_GET_CONTENT);
music.setType("audio/*");
startActivity(Intent.createChooser(music, "Select music"));
break;
case 2:
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:13428720000"));
startActivity(dial);
break;
case 3:
Intent wallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(wallpaper, "Select Wallpaper"));
break;
default :
break;
}
}
}
这段代码用三个按钮实现了三个Intent意图:音乐播放、自动拨号、背景选择。只用了一个onClick处理,这样代码看起来简洁了很多。
备注,Intent的属性写法与常数写法:
属性写法
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
常数写法
Intent wallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent music = new Intent(Intent.ACTION_GET_CONTENT);
在Intent类中,定义了action的常数。在记忆技巧上,可以用 xxx对应到ACTION_xxx 的方式记。例如:
CALL(android.intent.action.CALL)就是ACTION_CALL(Intent.ACTION_CALL)。
程序运行效果为:
[java] view
plaincopy
package com.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentSelectActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.btn1);
Button button2 = (Button)findViewById(R.id.btn2);
Button button3 = (Button)findViewById(R.id.btn3);
button1.setOnClickListener(this);
button1.setTag(1);
button2.setOnClickListener(this);
button2.setTag(2);
button3.setOnClickListener(this);
button3.setTag(3);
}
public void onClick(View v){
int tag = (Integer) v.getTag();
switch(tag){
case 1:
Intent music = new Intent(Intent.ACTION_GET_CONTENT);
music.setType("audio/*");
startActivity(Intent.createChooser(music, "Select music"));
break;
case 2:
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:13428720000"));
startActivity(dial);
break;
case 3:
Intent wallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(wallpaper, "Select Wallpaper"));
break;
default :
break;
}
}
}
这段代码用三个按钮实现了三个Intent意图:音乐播放、自动拨号、背景选择。只用了一个onClick处理,这样代码看起来简洁了很多。
备注,Intent的属性写法与常数写法:
属性写法
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
常数写法
Intent wallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent music = new Intent(Intent.ACTION_GET_CONTENT);
在Intent类中,定义了action的常数。在记忆技巧上,可以用 xxx对应到ACTION_xxx 的方式记。例如:
CALL(android.intent.action.CALL)就是ACTION_CALL(Intent.ACTION_CALL)。
程序运行效果为:
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories