安卓Button控件OnClickListener的三种实现方式
来源:互联网 发布:怎样写淘宝客招募 编辑:程序博客网 时间:2024/05/17 16:03
学习过一些Button控件的使用方法,对OnClickListener事件的定义方式有所不同,下面来总结一下该事件的三种使用方式:在xml文件中定义事件;在Actitivy中实例化一个事件;Activity实现该事件的接口。三种Button点击事件的实现,各有利弊,读者可以根据情况选择合理的使用方式。
1. 在xml文件中定义事件,这种方式适用于指定的Button控件,代码比较简洁。
xml文件:
<Button android:text="按钮"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="buttonClick"
</Button>
Activity类:
public void buttonClick(View v){
Intent intent = new Intent(mainActivity.this, otherActivity.class);
intent.putExtra("string", "info");
startActivity(intent);
}
2. 在Actitivy中实例化一个事件,该方法如果和第一种方法同时使用的话,则优先执行第一种方法。
xml文件:
<Button android:text="按钮"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</Button>
Activity类:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(mainActivity.this, otherActivity.class);
intent.putExtra("string", "info");
startActivity(intent);
}
});
}
这种方式使用起来比较方便,但是效率很低,尤其是写在ListView的适配器的getView方法中,需要创建多个事件对象,代码臃肿,浪费资源。解决办法如下。
xml文件:
<Button android:text="按钮"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</Button>
Activity类:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(listener);
}
OnClickListener listener=new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(mainActivity.this, otherActivity.class);
intent.putExtra("string", "info");
startActivity(intent);
}
});
需要有多个Button的点击事件的时候代码如下。
xml文件:
<Button android:text="按钮1"
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</Button>
<Button android:text="按钮2"
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</Button>
Activity类:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(listener);
}
OnClickListener listener= new OnClickListener(){
switch (v.getId()) {
case R.id.button1:
Intent intent = new Intent(mainActivity.this, otherActivity.class);
intent.putExtra("string1", "info1");
startActivity(intent);
break;
case R.id.button2:
Intent intent = new Intent(mainActivity.this, otherActivity1.class);
intent.putExtra("string2", "info2");
startActivity(intent);
break;
}
}
};
3.Activity实现该事件的接口,这是比较常用的一种使用方式,能够使同一个Activity使用一个相同点击事件,完成相应的操作。
xml文件:
<Button android:text="按钮1"
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</Button>
<Button android:text="按钮2"
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</Button>
Activity类:
public class mainActivity extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent intent = new Intent(mainActivity.this, otherActivity.class);
intent.putExtra("string1", "info1");
startActivity(intent);
break;
case R.id.button2:
Intent intent = new Intent(mainActivity.this, otherActivity1.class);
intent.putExtra("string2", "info2");
startActivity(intent);
break;
}
}
}
- 安卓Button控件OnClickListener的三种实现方式
- Android OnClickListener 的三种实现方式
- Android OnClickListener 的三种实现方式
- button的OnClickListener的三种实现方法
- button的OnClickListener的三种实现方法
- andorid中Button的OnClickListener的三种实现方法
- button的OnClickListener的三种实现方法
- button的OnClickListener的三种实现方
- button的OnClickListener的三种实现方法
- button的OnClickListener的三种实现方法
- Button的onClickListener的三种实现方法
- button的OnClickListener的三种实现方法
- button的OnClickListener的三种实现方法
- button的OnClickListener的三种实现方法
- Button实现OnClickListener事件的三种方法
- Android学习(4)Button的OnClickListener的三种实现方法
- OnClickListener的三种实现方法
- Android中button实现onclicklistener事件的两种方法
- python学习(一):环境安装及HelloWorld
- 机房收费系统开发进度月报
- 源码编译安装runc碰到的一个小坑
- Filter实现登录功能限制
- PAT(甲级)1047
- 安卓Button控件OnClickListener的三种实现方式
- C语言复习day2
- PAT(甲级)1048
- WPF 中图标路径问题
- jqery实现10X10的表格,双击消失
- 停课的沉淀反思(四)——任道重远
- poj 1222
- 自定义的RPC的Java实现
- Bios工程师手边事—battery