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 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.
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.
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.