如果第一次运行该应用,我希望启动按钮btnAdd的点击事件。
我已阅读How can I click a button to launch an ItemSelected event of spinner control?
我该怎么办?谢谢!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unlock_main);
IniControls();
if (IsFirstRun){
Button btnAdd=(Button)findViewById(R.id.btnAdd);
//launch a clike event of the button btnADD
}
}
private void IniControls(){
findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent().setClass(getApplicationContext(),ui.UnlockAddWiFi.class);
startActivityForResult(intent, RESULT_ADDWIFI);
}
});
}
答案 0 :(得分:1)
嗯。我能想到的最简单方法是做出偏好。
private Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unlock_main);
IniControls();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
final Boolean IsFirstRun = sp.getBoolean("firsttime", true);
sp.edit().putBoolean("firsttime", false).apply();
if (IsFirstRun){
btnAdd.performClick();
}
}
private void IniControls(){
btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent().setClass(getApplicationContext(),ui.UnlockAddWiFi.class);
startActivityForResult(intent, RESULT_ADDWIFI);
}
});
}
答案 1 :(得分:0)
只需编写一个充当onClick()内容的方法,并将其放在onClick()侦听器和SharedPreferences中,如Solamum所建议的那样。
private Button mButton = null;
.
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.unlock_main);
initControls();
/****************************************************************/
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
final Boolean IsFirstRun = sp.getBoolean("firsttime", true);
sp.edit().putBoolean("firsttime", false).apply();
if (IsFirstRun){
myClickEventMethod();
}
}
private void myClickEventMethod(){
Intent intent = new Intent().setClass(getApplicationContext(),ui.UnlockAddWiFi.class);
startActivityForResult(intent, RESULT_ADDWIFI);
}
private void initControls(){
mButton = findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myClickEventMethod();
}
});
}