Java 如何从其他类调用onClick()
一级Java 如何从其他类调用onClick(),java,android,onclick,Java,Android,Onclick,一级 public class MyHeader { private Button button1,button2; public MyHeader(Activity source) { this.button1 = (Button)source.findViewById(R.id.b_one); this.button2 = (Button)source.findViewById(R.id.b_two); // ... } public Button getHe
public class MyHeader {
private Button button1,button2;
public MyHeader(Activity source) {
this.button1 = (Button)source.findViewById(R.id.b_one);
this.button2 = (Button)source.findViewById(R.id.b_two);
// ...
}
public Button getHeaderButtonOne() { return button1; }
// And so on...
二级
private MyHeader header;
// ... in onCreate() method
header = new MyHeader(this);
使用这段代码,在第2类中,我能够访问在第1类中获得id的按钮。现在,我想为类1中设置的按钮获取onclick事件,如果我在类2中获取按钮,我也应该能够使用click事件。只需在类1中添加一个新方法即可:
public void clickButton1(){
button1.performClick();
}
从你的二班中这样称呼它:
header.clickButton1();