我目前正試圖應用一種「切換」類型的系統,根據它代表的布爾值來切換按鈕的文本。立即更改按鈕的文本
例如,當布爾型RequestingLU爲true時,我希望按鈕說'停止',如果爲false,則說'開始'。
目前我已經把它放在onStart();因此,它至少會更新,每當我訪問屏幕,
public void onStart() {
super.onStart();
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
final SharedPreferences.Editor editor = settings.edit();
final Button StopButton = (Button) findViewById(R.id.StopButton);
boolean RequestingLU = settings.getBoolean("RequestingLU", true);
if (RequestingLU) {
StopButton.setText(R.string.Stop_Button);
StopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainMenu.this);
StopDialog.setTitle(R.string.Stop_Title);
StopDialog.setMessage(R.string.Stop_Message);
StopDialog.setPositiveButton(R.string.Stop_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
editor.putBoolean("RequestingLU", false);
editor.apply();
Toast.makeText(MainMenu.this, "You have stopped the app", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
dialog.dismiss();
}
});
StopDialog.create().show();
}
});
}
else {
StopButton.setText(R.string.Start_Button);
StopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editor.putBoolean("RequestingLU", true);
editor.apply();
Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show();
}
});
}
}
我想要的按鈕功能,使這一變化在用戶按下後立即適用「解僱」。我怎樣才能做到這一點?
這是怎麼jQuery的? – Iceman
Woops,我點了一個錯誤的; /對不起。 –