//为控件绑定监听器
//1.获取代表控件的对象
private Button button;
button = (Button) findViewById(R.id.button1);
//2.定义一个类,实现监听器接口
class ButtonLister implements OnClickListener {
@Override
// 每点击Button一次,TextView上的字符串0,加1
public void onClick(View v) {
count++;
textView.setText(count+"");//转为字符串
}
}
//3.生成监听器对象
//4.为控件绑定监听器对象
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);//1.获取代表控件的对象
ButtonLister buttonLister=new ButtonLister();//3.生成监听器对象
button.setOnClickListener(buttonLister);//4.为控件绑定监听器对象
}
安卓入门.为控件绑定监听器
最新推荐文章于 2024-10-08 10:48:53 发布
本文详细介绍了在Android应用中为控件绑定监听器的步骤,包括获取控件对象、创建监听器类、生成监听器实例及将监听器绑定到控件。通过示例代码展示了如何实现点击事件响应。
475

被折叠的 条评论
为什么被折叠?



