查了网上的一些资料,加上个人的了解,写成了这篇博客
博客来自:http://blog.csdn.net/liuxian13183
在Android1.6里面,添加Listener的工作变得相当的简单(感觉更像在做网页编程!),具体步骤如下:
1.首先在layout里面定义Button并指定响应的Listener
<?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>
其中以下这两行就是新增的特性:
android:onClick="myClickHandler01"
android:onClick="myClickHandler02"
2.在活动里面定义public的方法myClickHandler01、和myClickHandler02(注意这两个方法必须有一个
View的形参)。
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"
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;
}
}
}
相当的好用!