我目前正在尝试使用'toggle'类型的系统来切换按钮的文本,具体取决于它所代表的布尔值。
例如,当boolean 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();
}
});
}
}
我希望按钮正常运行,以便在用户按下“关闭”后立即应用更改。我怎样才能做到这一点?
答案 0 :(得分:1)
我希望它对您有用 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);
StopButton.setText(RequestingLU?R.string.Stop_Button:R.string.Start_Button);
StopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final boolean RequestingLUSwitch = settings.getBoolean("RequestingLU", true);
if(RequestingLUSwitch) {
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) {
updatePreference(!RequestingLUSwitch);
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{
updatePreference(!RequestingLUSwitch);
Toast.makeText(MainMenu.this, "You have started the app", Toast.LENGTH_SHORT).show();
}
}
});
}
private void updatePreference(boolean requestingSwitch){
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("RequestingLU", !RequestingLUSwitch);
editor.apply();
StopButton.setText(!RequestingLUSwitch ? R.string.Stop_Button : R.string.Start_Button);
}
答案 1 :(得分:0)
将您的StopButton.setText()
放入onClicks