1.定义接口
public interface OnClickListener {
public void OnClick(Button b);
}
2. 定义Button
public class Button {
OnClickListener listener;
public void click() {
listener.OnClick(this);
}
public void setOnClickListener(OnClickListener listener) {
this.listener = listener;
}
}
3. 将接口对象OnClickListener 赋给 Button的接口成员
public class Activity {
public Activity() {
}
public static void main(String[] args) {
Button button = new Button();
button.setOnClickListener(new OnClickListener(){
@Override
public void OnClick(Button b) {
System.out.println("clicked");
}
});
button.click(); //user click,System call button.click();
}
}