[英]Click button from method that is outside the oncreate in Android
The following is a basic striped down overview of one of my projects. 以下是我的一个项目的基本概述。 I would like for a method outside of the oncreate to be able to click a button that is declaired in the oncreate but i cannot figure out how.
我希望oncreate之外的方法能够单击oncreate中定义的按钮,但是我不知道如何操作。
Here is my code: I would like to be able to performClick()
the webButton
from the method clickButton()
but it does not work. 这里是我的代码:我希望能够
performClick()
的webButton
从方法clickButton()
但它不工作。 If that is not possible i would atleast like to know how to start the intent that is trying to be started when the button is clicked from instide the clickButton()
method. 如果那是不可能的,那么我至少想知道如何启动从
clickButton()
方法中单击按钮时试图启动的意图。
Thank you in advance!! 先感谢您!!
public class cphome extends Activity {
static final int check =111;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button webButton = (Button) findViewById(R.id.button1);
webButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), web.class);
startActivity(myIntent);
}
});
}
public void clickbutton(){
webButton.performClick();
}
}
This is a really strange pattern. 这是一个非常奇怪的模式。 I would suggest you to do something like that instead:
我建议您改为执行以下操作:
public class cphome extends Activity {
static final int check =111;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button webButton = (Button) findViewById(R.id.button1);
webButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
performClickAction();
}
});
}
public void performClickAction(){
Intent myIntent = new Intent(this, web.class);
startActivity(myIntent);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.