Android-setOnClickListener与OnClickListener与View.OnClickListener [英] Android - setOnClickListener vs OnClickListener vs View.OnClickListener
问题描述
我的理解是,当我创建一个监听点击的按钮对象时,我必须:
My understanding is that when I'm creating a button object that listens for a click, I have to:
- 创建按钮对象
- 使用
OnClickListner
使其监听用户的点击 - 在用户单击按钮后使用
onClick
执行操作
- Create the button object
- Use
OnClickListner
to make it listen to the user's click - Use
onClick
to execute actions after the user clicks the button
现在,
-
setOnClickListener
在哪里适合上述逻辑? - 实际上是哪个人在听按钮点击?
-
setOnClickListener
? -
OnClickListener
? -
View.OnClickListener
? - 这三个之间有什么区别?
- Where does
setOnClickListener
fit into the above logic? - Which one actually listens to the button click?
setOnClickListener
?OnClickListener
?View.OnClickListener
?- What are the differences between those three?
这是我在Android Dev中找到的内容:
Here is what I found in Android Dev:
//The example below shows how to register an on-click listener for a Button.
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};
protected void onCreate(Bundle savedValues) {
...
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
// Register the onClick listener with the implementation above
button.setOnClickListener(mCorkyListener);
...
}
您可能还会发现,将OnClickListener
作为您的Activity
一部分的实现更为方便.这将避免额外的类负载和对象分配.例如:
You may also find it more convenient to implement OnClickListener
as a part of your Activity
. This will avoid the extra class load and object allocations. For example:
public class ExampleActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedValues) {
...
Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(this);
}
// Implement the OnClickListener callback
public void onClick(View v) {
// do something when the button is clicked
}
}
推荐答案
逻辑很简单. setOnClickListener
属于步骤2.
The logic is simple. setOnClickListener
belongs to step 2.
- 您创建按钮
- 您可以像在该示例中一样创建
OnClickListener
*的实例,并覆盖onClick
-方法. - 您可以在片段/活动
onCreate
-方法中使用btn.setOnClickListener(myOnClickListener);
将该OnClickListener
分配给该按钮. - 当用户单击按钮时,将调用分配的
OnClickListener
的onClick
功能.
- You create the button
- You create an instance of
OnClickListener
* like it's done in that example and override theonClick
-method. - You assign that
OnClickListener
to that button usingbtn.setOnClickListener(myOnClickListener);
in your fragments/activitiesonCreate
-method. - When the user clicks the button, the
onClick
function of the assignedOnClickListener
is called.
*如果您使用import android.view.View;
,则使用View.OnClickListener
.如果您import android.view.View.*;
或import android.view.View.OnClickListener;
,请按照我的理解使用OnClickListener
.
*If you import android.view.View;
you use View.OnClickListener
. If you import android.view.View.*;
or import android.view.View.OnClickListener;
you use OnClickListener
as far as I get it.
另一种方法是让您的活动/片段继承自OnClickListener
.这样,您可以将片段/活动分配为按钮的侦听器,并将onClick
实现为成员函数.
Another way is to let you activity/fragment inherit from OnClickListener
. This way you assign your fragment/activity as the listener for your button and implement onClick
as a member-function.
这篇关于Android-setOnClickListener与OnClickListener与View.OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!