I want different objects to have different methods
For example how to make following java pseudocode work?
Button b[] = new Button[5];
button[0].onClick = new method(arguments){
Log.i("a", "button0");
return true;
}
button[1].onClick = new method(arguments){
Log.i("a", "button1");
return false;
}
button[1].onClick(123);
UPD1: Guys, what about following code I found on the internet. How to make this Button class and write function similar to setOnClickListener? PS: Button is my own class and it is not taken from default libs.
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Log.i("a", "Yohoho");
}
});
You can't do that exactly, but you could have:
button.addClickHandler(clickHandler);
where clickHandler
could actually be a lambda expression implementing some ClickHandler
interface.
The Button
class itself would be responsible for calling that handler at the right time though, e.g. you might call button.click()
and it call the handler for you.
See more on this question at Stackoverflow