Android setOnClickListener 方法 - 它是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25803727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android setOnClickListener method - How does it work?
提问by rayleigh
I have trouble understanding this code. I get that findViewByIdwill get the button widget and then it'll cast it. Then, it's going to use the button to call the setOnClickListenermethod. However, I don't know what is that argument being passed into the setOnClickListenerand I have never seen code like that before. How is it that it creates a new object but is able to create a method of its own within another method's argument? Would be great if someone could explain that. Also, what type of object is the setOnClickListenermethod taking in?
我无法理解此代码。我知道这findViewById将获得按钮小部件,然后它会投射它。然后,它将使用按钮来调用该setOnClickListener方法。但是,我不知道传递给 的参数是什么,setOnClickListener而且我以前从未见过这样的代码。它如何创建一个新对象但能够在另一个方法的参数中创建自己的方法?如果有人能解释一下,那就太好了。另外,该setOnClickListener方法接受什么类型的对象?
btn = (Button)findViewById(R.id.firstButton);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tv.setText(months[rand.nextInt(12)]);
tv.setTextColor(Color.rgb(rand.nextInt(255)+1, rand.nextInt(255)+1, rand.nextInt(255)+1));
}
});
采纳答案by cliffroot
It works like this. View.OnClickListenere is defined -
它是这样工作的。View.OnClickListenere 已定义 -
public interface OnClickListener {
void onClick(View v);
}
As far as we know you cannot instantiate an object OnClickListener, as it doesn't have a method implemented. So there are two ways you can go by - you can implement this interface which will override onClickmethod like this:
据我们所知,你不能实例化一个对象OnClickListener,因为它没有实现一个方法。因此,您可以通过两种方法 - 您可以实现此接口,该接口将覆盖这样的onClick方法:
public class MyListener implements View.OnClickListener {
@Override
public void onClick (View v) {
// your code here;
}
}
But it's tedious to do it each time as you want to set a click listener. So in order to avoid this you can provide the implementation for the method on spot, just like in an example you gave.
但是每次都要设置一个点击监听器,这很乏味。因此,为了避免这种情况,您可以在现场提供该方法的实现,就像您提供的示例一样。
setOnClickListenertakes View.OnClickListeneras its parameter.
setOnClickListener将View.OnClickListener其作为参数。
回答by nafees ahmed
This is the best way to implement Onclicklistener for many buttons in a row implement View.onclicklistener.
这是为连续实现 View.onclicklistener 的许多按钮实现 Onclicklistener 的最佳方式。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
This is a button in the MainActivity
这是 MainActivity 中的一个按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_submit = (Button) findViewById(R.id.submit);
bt_submit.setOnClickListener(this);
}
This is an override method
这是一个覆盖方法
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.submit:
//action
break;
case R.id.secondbutton:
//action
break;
}
}
回答by Pintspin
its an implementation of anonymouse class object creation to give ease of writing less code and to save time
它是匿名类对象创建的实现,可以轻松编写更少的代码并节省时间
回答by Developer
That what manual saysabout setOnClickListenermethod is:
那什么手动说,有关setOnClickListener的方法是:
public void setOnClickListener (View.OnClickListener l)
Added in API level 1 Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.
在 API 级别 1 中添加注册一个回调以在单击此视图时调用。如果此视图不可点击,它将变为可点击。
Parameters
参数
l View.OnClickListener: The callback that will run
l View.OnClickListener:将运行的回调
And normally you have to use it like this
通常你必须像这样使用它
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
}
...
}
Take a look at this lesson as well Building a Simple Calculator using Android Studio.
看看这节课以及使用 Android Studio 构建一个简单的计算器。

