【问题标题】:How can I save the state of my Switch using SharedPreferences?如何使用 SharedPreferences 保存 Switch 的状态?
【发布时间】:2018-09-24 19:08:40
【问题描述】:

我实现了两个开关,一次只能有一个为真,现在我正在尝试保存开关的状态。我查看了与我类似的其他 StackOverflow 问题,但有些问题不起作用。这是我的代码:

public class StartingActivity extends AppCompatActivity
{

private Switch hourly, salary;
private Boolean hrlySwitch, slrySwitch;
private Double hrly, slry, tax;
private SharedPreferences pref;

private static  String TAG = "tag";

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starting);

    pref = getApplicationContext().getSharedPreferences("switchInfo", MODE_PRIVATE);

    hourly = (Switch)findViewById(R.id.hourly);
    salary = (Switch)findViewById(R.id.salary);

    //get Bool SharedPreference
    hourly.setChecked(pref.getBoolean("hrlyBool", false));
    salary.setChecked(pref.getBoolean("slryBool", true));

    //switching
    salary.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if (salary.isChecked())
            {
                hourly.setChecked(false);
                slrySwitch = true;
                hrlySwitch = false;
            }
            else
            {
                hourly.setChecked(true);
                slrySwitch = false;
                hrlySwitch = true;
            }
        }
    });

    hourly.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if (hourly.isChecked())
            {
                salary.setChecked(false);
                slrySwitch = false;
                hrlySwitch = true;
            }
            else
            {
                salary.setChecked(true);
                slrySwitch = true;
                hrlySwitch = false;
            }
        }
    });

    Log.d(TAG,"before switch rule");
    //put switch rule
    SharedPreferences.Editor editor = pref.edit();
    Log.d(TAG,"pref.edit()");
    editor.putBoolean("hrlyBool",hrlySwitch);
    Log.d(TAG,"putBool hourly");
    editor.putBoolean("slryBool", slrySwitch);
    Log.d(TAG,"putBool salary");
    editor.commit();
    Log.d(TAG,"after switch rule");

}

} 

我包含了日志语句来跟踪我的应用程序崩溃的位置,执行的最后一行代码是“pref.edit()”日志语句。但我不确定为什么我的 putBoolean 会导致问题。

【问题讨论】:

    标签: android sharedpreferences uiswitch


    【解决方案1】:

    你不能这样做吗?

    public class StartingActivity extends AppCompatActivity
    {   
        private static final String SWITCH_PREFS = "switchInfo";
        private static final String HOURLY_PREF = "hrlyBool";
        private static final String SALARY_PREF = "slryBool";
    
        private Switch hourly, salary;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_starting);
    
            hourly = (Switch)findViewById(R.id.hourly);
            salary = (Switch)findViewById(R.id.salary);
    
            hourly.setChecked(getApplicationContext()
                    .getSharedPreferences(StartingActivity.SWITCH_PREFS, MODE_PRIVATE)
                    .getBoolean(StartingActivity.HOURLY_PREF, true));
            salary.setChecked(getApplicationContext()
                    .getSharedPreferences(StartingActivity.SWITCH_PREFS, MODE_PRIVATE)
                    .getBoolean(StartingActivity.SALARY_PREF, false));
    
            salary.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    hourly.setChecked(!isChecked);
                    saveSwitchStates();
                }
            });
    
            hourly.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    salary.setChecked(!isChecked);
                    saveSwitchStates();
                }
            });
        }
    
        private void saveSwitchStates() {
            SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(StartingActivity.SWITCH_PREFS,
                    MODE_PRIVATE).edit();
            editor.putBoolean(StartingActivity.HOURLY_PREF, hourly.isChecked());
            editor.putBoolean(StartingActivity.SALARY_PREF, salary.isChecked());
            editor.commit();
        }
    } 
    

    我不确定你为什么将所有这些值保存在 StartingActivity 类中,当你想知道它是否被检查时,只需使用开关 isChecked 函数,侦听器只需要交换其他值。

    【讨论】:

    • 谢谢!这行得通。这是我第一次使用共享偏好,所以我不太熟悉如何去做。而我分配和保存其他变量(slrySwitch、hrlySwitch)的原因是因为我将这些值传递给另一个 Activity 以确定其他一些事情。
    • 您知道您可以从其他活动的共享首选项中提取它们。我会将共享首选项分离为您的模型,然后让演示者/控制器从模型中提取,执行业务逻辑并将其传递给视图(MVC/MVVM)。
    • @Popmedic 你能告诉我b.setChecked(!b) 中的值是多少
    • Opps,只是复制错误,我觉得应该是.setChecked(!isChecked)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2016-06-09
    • 1970-01-01
    相关资源
    最近更新 更多