Onclicklistener для изменения пользовательского интерфейса не обновляется при первом нажатии - PullRequest
0 голосов
/ 07 июля 2019

У меня есть проблема, когда я пытаюсь изменить передний план кнопки после события щелчка.Передний план изменяется только после первого щелчка.

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, которую я использую, чтобы определить, какой передний план использовать.

Ответы [ 2 ]

1 голос
/ 07 июля 2019

Сначала вам нужно проверить PlaySound в начале упражнения, чтобы сама кнопка получала то, что вы сохраняете в первый раз, поэтому вам нужно что-то вроде этого:

    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 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();
    }
}

}
0 голосов
/ 07 июля 2019

Похоже, вы не редактируете настройки. Вот так.

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();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...