android - 更改 Android 开关状态

标签 android android-activity uiswitch

我目前正在开发一个有菜单的应用程序,菜单上的一个选项是“设置”,用户基本上可以在其中决定关闭声音和其他类似的东西。我目前在设置 Activity 中有两个开关。到目前为止,这是设置 Activity 的 Java 代码:

import android.support.v7.app.ActionBarActivity;


public class Options extends ActionBarActivity {
private  Switch ding;
private Switch countdown;
public  boolean isDingChecked;
public  boolean isCountdownChecked;
public static final String PREFS = "examplePrefs";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
ding = (Switch) findViewById(R.id.switch1);
ding.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        SharedPreferences examplePrefs = getSharedPreferences(PREFS,0);
        Editor editor = examplePrefs.edit();
        editor.putBoolean("userMessage", isChecked);
        editor.commit();

        //System.out.println(examplePrefs.getBoolean("userMessage", isChecked));
        isDingChecked = examplePrefs.getBoolean("userMessage", isChecked);
        System.out.println(isDingChecked + " is ding checked");
        ding.setChecked(isDingChecked);
    }
});

countdown = (Switch) findViewById(R.id.switch2);
countdown.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // do something, the isChecked will be
        // true if the switch is in the On position
        isCountdownChecked = isChecked;

    }
});     
 }
 }

我可以在我的其他 Activity 中使用 bool 值,因此 SharedPreference 工作正常。然而,当我回到我的菜单 Activity 并回到这个选项 Activity 时,开关的状态回到它的默认值 true,不管用户声明什么。无论如何我可以解决这个问题吗?

ding.setChecked(isDingChecked)

我想这并没有真正做任何事情。我知道我过去发布过一个类似的问题,只是没有太多 Activity ,我已经在这个问题上工作了很长一段时间。谢谢!

最佳答案

尝试这样的事情:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);
    ding = (Switch) findViewById(R.id.switch1);

    //grab prefs first
    final SharedPreferences examplePrefs = getSharedPreferences(PREFS,0);
    final Editor editor = examplePrefs.edit();
    ding.setChecked(examplePrefs.getBoolean("your_key", false)); //false default 


    ding.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //commit prefs on change
            editor.putBoolean("your_key", isChecked);
            editor.commit();

            System.out.println(isDingChecked + " is ding checked");
        }
    });

关于android - 更改 Android 开关状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945742/

相关文章:

android - 禁用android中的 Activity 幻灯片转换更改?

ios - UISwitch 在 Xcode 9.3 的模拟器中损坏?

ios - 在 iOS7 上更改 UISwitch 的 onTintColor?

android - 使用共享 ViewModel 的多个 fragment 中的单个实时事件

android - 每次打开时更新 MainActivity 中的值

android - 强制 Firebase 使用缓存数据

android - 如何获取另一个尚未开始的 Activity 的上下文?

c# - 将自定义对象传递给 Xamarin Android 中的下一个 Activity

ios - 隐藏目标 VC-Swift 中的开关

android - 如何通过长时间运行的 onDestroy 来管理停止服务?