对于点击监听器通用类 [英] Common class for click listener
本文介绍了对于点击监听器通用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我所有的6项活动7键。所有6个按钮在所有活动中相同的功能。我怎么能执行一个共同的单击事件lisnter为这6个按键。
I have 7 buttons in all my 6 activities. All 6 buttons have the same functionality in all activities. How can I perform a common click event lisnter for these 6 buttons.
推荐答案
您可以创建一个实现View.OnClickListener这样一个新的类:
You can create a new class that implements View.OnClickListener like this:
public class MyClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
// TODO handle the click
}
}
在所有的活动,你可以再设置点击监听器是这样的:
In all your activities you can then set the click listener like this:
button.setOnClickListener(new MyClickListener());
您甚至可以保存在类的上下文显示敬酒等。
You could even save the context in the class for displaying Toasts etc.
public class MyClickListener implements View.OnClickListener {
private Context context;
public MyClickListener(Context context) {
this.context = context;
}
@Override
public void onClick(View view) {
Button button = (Button) view;
Toast.makeText(this.context, button.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
这篇关于对于点击监听器通用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文