Android View添加 Listener 小技巧示例
2010-06-19 17:48:00 来源:WEB开发网android:layout_height="wrap_content"
android:text="@string/hello"
/>
< /LinearLayout>
其中以下这两行就是新增的特性:
android:
android:
2.在活动里面定义public的方法myClickHandler01、和myClickHandler02(注意这两个方法必须有一个View的形参)。
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:
android:
Java代码
package com.ray.test;
import android.app.Activity;
import android.os.Bundle;
赞助商链接