更改 Android 开关状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25945742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:51:57  来源:igfitidea点击:

Change Android Switch State

androidandroid-activityuiswitch

提问by user2677095

I currently am developing an app that has a menu, and one of the options on the menu is "Settings" where the user can basically decide to turn off sounds and other things like that. I currently have two switches in the Settings activity. Here is the java code for the Settings activity so far:

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

    }
});     
 }
 }

I am able to use the boolean values in my other activity, so the SharedPreference works fine. However, when I go back to my menu activity and go back to this options activity, the state of the switches goes back to its default values of being true, regardless of what the user states. Is there anyway I can fix this?

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

ding.setChecked(isDingChecked)

Isn't really doing anything I guess. I know I posted a question similar to this in the past, it's just that one hasn't had much activity and I've been working on this issue for quite some time. Thanks!

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

回答by Will Thomson

Try something like this:

尝试这样的事情:

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

回答by reVerse

The setChecked(value)method of the Switch works totally fine but you're calling it inside the onCheckedChanged(...)method which is unnecessary.
So in order to set the Switchto the latest value you should load the Preferences and set the checked state outside of the setOnCheckedChangeListenerlistener.

setChecked(value)Switch的方法完全正常,但您在onCheckedChanged(...)方法中调用它是不必要的。
因此,为了将 设置Switch为最新值,您应该加载首选项并在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);
}