如何使用SharedPreferences保存Switch的状态?

时间:2018-04-14 16:32:41

标签: android sharedpreferences uiswitch

我实现了两个Switch,其中一次只能有一个True,现在我正在尝试保存交换机的状态。我查看了类似于我的其他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");

}

} 

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

1 个答案:

答案 0 :(得分: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(!b);
                saveSwitchStates();
            }
        });

        hourly.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                salary.setChecked(!b);
                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函数就可以知道它是否已被选中,并且侦听器只需要交换其他值。