The class Viewhas an interface, and it OnClickListener, it looks like this in the source View.java:
public interface OnClickListener {
void onClick(View v);
}
Usually you should create a class and implement this interface:
public void MyClass implements View.OnClickListener {
@Override
public void onClick(View view) {
}
}
But sometimes you do not need this class in a separate file. Instead, you can create an anonymous inner class, this is like creating a new class in which only methods are specified from the specified interface:
new View.OnClickListener() {
@Override
public void onClick(View v){
ourMessage.setText("The button got tapped");
}
}
, View.OnClickListener.
, , . , :
public class MyClass {
private int clicksCount = 0;
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
clicksCount += 1;
}
}
}
clicksCount, MyClass , OnClickListener. - , final:
public void testMethod(final int canAccess, int cantAccess) {
final String test = otherView.getText().toString();
myView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (test.length == 0) {
}
}
}