更改Android Switch状态

时间:2014-09-20 05:52:57

标签: android android-activity uiswitch

我目前正在开发一个带有菜单的应用程序,菜单上的一个选项是"设置"用户基本上可以决定关闭声音和其他类似的东西。我目前在“设置”活动中有两个开关。以下是目前为止设置活动的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;

    }
});     
 }
 }

我可以在我的其他活动中使用布尔值,因此SharedPreference可以正常工作。但是,当我返回到菜单活动并返回到此选项活动时,无论用户声明什么,开关的状态都会恢复为默认值true。无论如何我可以解决这个问题吗?

ding.setChecked(isDingChecked)

我猜是不是真的做了什么。我知道我过去发过一个与此类似的问题,只是一个人没有多少活动,而且我已经在这个问题上工作了很长一段时间。谢谢!

3 个答案:

答案 0 :(得分:19)

尝试这样的事情:

    @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");
        }
    });

答案 1 :(得分:0)

Switch的setChecked(value)方法完全正常,但您在onCheckedChanged(...)方法中调用它是不必要的。
因此,为了将Switch设置为最新值,您应该加载Preferences并在setOnCheckedChangeListener侦听器之外设置已检查状态。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_options);
    // your current code

    //load and set preferences
    SharedPreferences examplePrefs = getSharedPreferences(PREFS,0);
    isDingChecked = examplePrefs.getBoolean("userMessage", isChecked);
    ding.setChecked(isDingChecked);
}

答案 2 :(得分:0)

科特琳当然回答:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_options)
    val examplePrefs = getSharedPreferences(PREFS, 0)
    val editor = examplePrefs.edit()
    ding.setChecked(examplePrefs.getBoolean("your_key", false))
    
    ding.setOnCheckedChangeListener{_, checked ->
        examplePrefs.putBoolean("your_key", checked).apply()
    }
}