android 1.6简便添加监听器的方法(转)
2011-01-25 10:28
295 查看
android 1.6简便添加监听器的方法
文章分类:移动开发在开发中为控件添加Listener是非常常见的工作,最简单的添加Listener方式可以这样:
Java代码
Java代码
findViewById(R.id.myButton).setOnClickListener(
new
View.OnClickListener() {
public
void
onClick(View v) {
// Do stuff
}
});
findViewById(R.id.myButton).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do stuff } });
采用上述方法添加Listener有个缺点就是如果控件太多的话,Listener数量也会增多,因此,可以采用如下的小窍门减少Listener的数量:
Java代码
Java代码
View.OnClickListener handler = View.OnClickListener() {
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.Button01:
// doStuff
break
;
case
R.id.Button02:
// doStuff
break
;
}
}
}
findViewById(R.id.myButton).setOnClickListener(handler);
findViewById(R.id.myOtherButton).setOnClickListener(handler);
View.OnClickListener handler = View.OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.Button01: // doStuff break; case R.id.Button02: // doStuff break; } } } findViewById(R.id.myButton).setOnClickListener(handler); findViewById(R.id.myOtherButton).setOnClickListener(handler);
在Android1.6里面,添加Listener的工作变得相当的简单(感觉更像在做网页编程!),具体步骤如下:
1.首先在layout里面定义Button并指定响应的Listener
Xml代码
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:text
=
"Button01"
android:id
=
"@+id/Button01"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:onClick
=
"myClickHandler01"
/>
<
Button
android:text
=
"Button02"
android:id
=
"@+id/Button02"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:onClick
=
"myClickHandler02"
/>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello"
/>
</
LinearLayout
>
<?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:text="Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myClickHandler01" /> <Button android:text="Button02" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="myClickHandler02" /> <TextView android:layout_width="fil b3aa l_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
其中以下这两行就是新增的特性:
android:onClick="myClickHandler01"
android:onClick="myClickHandler02"
2.在活动里面定义public的方法myClickHandler01、和myClickHandler02(注意这两个方法必须有一个View的形参)。
Java代码
Java代码
package
com.ray.test;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
public
class
TestOnClickListener
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public
void
myClickHandler01(View target){
setTitle("myClickHandler01"
);
}
public
void
myClickHandler02(View target){
setTitle("myClickHandler02"
);
}
}
package com.ray.test; import android.app.Activity; import android.os.Bundle; import android.view.View; public class TestOnClickListener extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void myClickHandler01(View target){ setTitle("myClickHandler01"); } public void myClickHandler02(View target){ setTitle("myClickHandler02"); } }
当然,你也可以采用这种写法:
将两个按钮设置到同一个Listener
android:onClick="myClickHandler"
android:onClick="myClickHandler"
Java代码
Java代码
package
com.ray.test;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
public
class
TestOnClickListener
extends
Activity {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public
void
myClickHandler(View target){
switch
(target.getId()) {
case
R.id.Button01:
setTitle("myClickHandler01"
);
break
;
case
R.id.Button02:
setTitle("myClickHandler02"
);
break
;
}
}
}
相关文章推荐
- android 简便添加监听器的方法
- 简便添加监听器的方法
- 关于在Android中添加事件监听器的方法
- Android实现RecyclerView添加分割线的简便方法
- Android开发:TextView添加超链接的简便方法
- Android开发:TextView添加超链接的简便方法
- Android手机通讯录批量添加方法
- 用ndk编译cocos2d-x的android版本的简便方法
- Android中添加自定义工具栏的方法
- android自定义控件并添加属性的方法以及示例
- Android动态添加设置布局与控件的方法
- cocos2dx3.0 添加第三方库,VS编译环境和Android编译环境添加方法
- Android - ExpandableListView的group里添加button后不能展开的解决方法
- Android为TextView添加字体库和设置描边的方法
- android自定义listview,添加监听器,解决屏幕滑动组件状态干扰的问题checkbox ...
- Android-day03--添加监听的第三种方法
- android 正确添加外部jar包方法
- 查看Android签名的简便方法
- android编程实现添加文本内容到sqlite表中的方法
- Android成长之路-实现监听器的三种方法