java Android - 如何在 button.setOnClickListener() 中设置命名方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1972579/
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 - How to set a named method in button.setOnClickListener()
提问by James Cadd
Most samples that I see appear to use an anonymous method in a call like button.setOnClickListener(). Instead, I'd like to pass in a method defined on the Activity class that I'm working in. What's the Java/Android equivalent of the following event handler wiring in C#?
我看到的大多数示例似乎都在像 button.setOnClickListener() 这样的调用中使用了匿名方法。相反,我想传入一个在我正在使用的 Activity 类上定义的方法。在 C# 中,以下事件处理程序接线的 Java/Android 等效项是什么?
Button myButton = new Button();
myButton.Click += this.OnMyButtonClick;
Where:
在哪里:
private void OnMyButtonClick(object sender, EventArgs ea)
{
}
Essentially, I'd like to reuse a non-anonymous method to handle the click event of multiple buttons.
本质上,我想重用一个非匿名方法来处理多个按钮的点击事件。
回答by GrkEngineer
Roman Nurik's answer is almost correct. View.OnClickListener() is actually an interface. So if your Activity implements OnClickListener, you can set it as the button click handler.
Roman Nurik 的回答几乎是正确的。View.OnClickListener() 实际上是一个接口。因此,如果您的 Activity 实现了 OnClickListener,则可以将其设置为按钮单击处理程序。
public class Main extends Activity implements OnClickListener {
public void onCreate() {
button.setOnClickListener(this);
button2.setOnClickListener(this);
}
public void onClick(View v) {
//Handle based on which view was clicked.
}
}
There aren't delegates as in .Net, so you're stuck using the function based on the interface. In .Net you can specify a different function through the use of delegates.
没有 .Net 中的委托,因此您无法使用基于接口的函数。在 .Net 中,您可以通过使用委托来指定不同的功能。
回答by Roman Nurik
The argument to View.setOnClickListenermust be an instance of the class View.OnClickListener(an inner class of the Viewclass).. For your use case, you can keep an instance of this inner class in a variable and then pass that in, like so:
参数View.setOnClickListener必须是类的实例(类View.OnClickListener的内部类View)。对于您的用例,您可以将此内部类的实例保留在变量中,然后将其传入,如下所示:
View.OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
// do something here
}
};
myButton.setOnClickListener(clickListener);
myButton2.setOnClickListener(clickListener);
If you need this listener across multiple subroutines/methods, you can store it as a member variable in your activity class.
如果您需要跨多个子例程/方法使用此侦听器,您可以将其作为成员变量存储在您的活动类中。
回答by Ryan Alford
The signature of the method will need to be this...
该方法的签名将需要是这样的......
public void onMyButtonClick(View view){
}
If you are not using dynamic buttons, you can set the "onClick" event from the designer to "onMyButtonClick". That's how I do it for static buttons on a screen. It was easier for me to relate it to C#.
如果您不使用动态按钮,则可以将设计器的“onClick”事件设置为“onMyButtonClick”。这就是我为屏幕上的静态按钮所做的。我更容易将它与 C# 联系起来。

