ui 更改的 onclicklistener 不会在第一次点击时更新
onclicklistener for ui change does not update on first click
我遇到了一个问题,我试图在单击事件时更改按钮的前景。前景仅在第一次点击后发生变化。
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.apply();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
我从 android SharedPrefernces 中检索一个变量,我用它来确定要使用的前景。
您似乎并没有真正编辑首选项。像这样。
SharedPreferences.Editor editor;
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
editor = sharedPreferences.edit();
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.commit();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
首先,您需要在 activity 开始时检查播放声音,以便按钮本身能够获得您第一次保存的内容,因此您需要这样的东西:
playSound = preferences.getBoolean("playSound", true);
if(playSound)
button.setForeground(getDrawable(R.drawable.foreground_padding_mute));
else
button.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
然后您可以使用您在问题中发布的相同方法处理点击
这里有一个示例 activity 总结一下:
public class SimpleActivity extends AppCompatActivity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
boolean playSound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
editor= PreferenceManager.getDefaultSharedPreferences(this).edit();
Button button = findViewById(YourButtonID);
playSound = preferences.getBoolean("playSound", true);
if(playSound)
button.setForeground(getDrawable(R.drawable.foreground_padding_mute));
else
button.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
}
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.apply();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
}
我遇到了一个问题,我试图在单击事件时更改按钮的前景。前景仅在第一次点击后发生变化。
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.apply();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
我从 android SharedPrefernces 中检索一个变量,我用它来确定要使用的前景。
您似乎并没有真正编辑首选项。像这样。
SharedPreferences.Editor editor;
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
editor = sharedPreferences.edit();
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.commit();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
首先,您需要在 activity 开始时检查播放声音,以便按钮本身能够获得您第一次保存的内容,因此您需要这样的东西:
playSound = preferences.getBoolean("playSound", true);
if(playSound)
button.setForeground(getDrawable(R.drawable.foreground_padding_mute));
else
button.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
然后您可以使用您在问题中发布的相同方法处理点击
这里有一个示例 activity 总结一下:
public class SimpleActivity extends AppCompatActivity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
boolean playSound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
editor= PreferenceManager.getDefaultSharedPreferences(this).edit();
Button button = findViewById(YourButtonID);
playSound = preferences.getBoolean("playSound", true);
if(playSound)
button.setForeground(getDrawable(R.drawable.foreground_padding_mute));
else
button.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
}
public void muteSoundClick(View view){
playSound = preferences.getBoolean("playSound", true);
if (playSound) {
view.setForeground(getDrawable(R.drawable.foreground_padding_unmute));
editor.putBoolean("playSound", false);
editor.apply();
}
else {
view.setForeground(getDrawable(R.drawable.foreground_padding_mute));
editor.putBoolean("playSound", true);
editor.apply();
}
}
}