gpt4 book ai didi

android - 立即更改按钮的文本

转载 作者:行者123 更新时间:2023-11-29 01:17:17 25 4
gpt4 key购买 nike

我目前正在尝试应用一种“切换”类型的系统,该系统根据按钮所代表的 bool 值切换按钮的文本。

例如,当 boolean RequestingLU 为真时,我希望按钮说“停止”,如果为假,则说“开始”。

目前我已经把它放在 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();
}
});
}

}

我希望按钮能够正常工作,以便在用户按下“关闭”后立即应用更改。我怎样才能做到这一点?

最佳答案

我希望它对你有用 public void onStart() { super .onStart(); 最终 SharedPreferences 设置 = getSharedPreferences(PREFS_NAME, 0); 最终 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);

关于android - 立即更改按钮的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com