声明一个类实现OnClickListener与自己申报呢? [英] Declaring that a class implements OnClickListener vs. declaring it yourself?
问题描述
道歉我的标题,我无法正确地阐明这个问题。
我所看到的 OnCLickListener
两种实现方式。首先是通过标志着你的类实现 OnCLickListener
完成。第二个通过让你自己声明它完成的任务。
为什么在第一个选项你能简单地把这个
为你的 setOnCLickListener
的说法,但在第二个你必须通过创建的麻烦 OnClickListener
反对自己呢?
第一:
公共类WidgetConfig扩展活动实现OnClickListener { @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.widgetconfig);
按钮B =(按钮)findViewById(R.id.bwidgetconfig);
b.setOnClickListener(本);
}
//的onClick了的onCreate之外定义
@覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根 }
第二:
公共类WidgetConfig延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.widgetconfig);
按钮B =(按钮)findViewById(R.id.bwidgetconfig);
b.setOnClickListener(BLISTENER);
}私人按钮BLISTENER =新OnClickListener(){b.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){ //去做 }
});
在第一种方法,您整个活动类实施 OnClickListener
接口。您可以在 OnClickListener
每个视图设置为此,并接受所有的点击事件的一种方法,在这里你就可以过滤它们并呼吁采取行动它们。
第二种方法使用的匿名内部类实现接口的方法。通过这种方法,您会收到事件只对特定视图。
在第一种方法,整个班级使用 OnClickListener
,传递给所有你想要监听的点击视图的一个实例。
第二种方法转换为:
Button.OnClickListener anonymous_listener =新Button.OnClickListener(){...};
button.setOnClickListener(anonymous_listener);
这就是说,它动态地创建并存储一个新的 OnClickListener
例如,当你使用它。
Apologies for my title, I am having trouble properly articulating the problem.
I have seen OnCLickListener
implemented in two ways. The first is done by signifying that your class implements OnCLickListener
. The second accomplishes the task by having you declare it yourself.
Why in the first option can you simply put this
as your setOnCLickListener
argument, but in the second you must go through the trouble of creating the OnClickListener
object yourself?
The first:
public class WidgetConfig extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetconfig);
Button b = (Button)findViewById(R.id.bwidgetconfig);
b.setOnClickListener(this);
}
//onClick defined outside of the onCreate
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
The Second:
public class WidgetConfig extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetconfig);
Button b = (Button)findViewById(R.id.bwidgetconfig);
b.setOnClickListener(bListener);
}
private Button bListener = new OnClickListener(){
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//TO-DO
}
});
In the first method, you entire Activity class implements the OnClickListener
interface. You can set the OnClickListener
of every View to this, and receive all the click events in one method, where you can then filter them and act upon them.
The second method uses an anonymous inner class that implements the interface method. By using this approach, you receive events only for that particular View.
In the first method, your entire class uses one single instance of the OnClickListener
, that is passed to all the Views you want to listen for clicks on.
The second method translates to:
Button.OnClickListener anonymous_listener = new Button.OnClickListener() { ... };
button.setOnClickListener(anonymous_listener);
Which is to say that it dynamically creates and stores a new OnClickListener
instance when you use it.
这篇关于声明一个类实现OnClickListener与自己申报呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!