使用切换按钮的共享偏好

时间:2016-05-25 13:08:34

标签: android switch-statement sharedpreferences

我有一个项目,我想在共享偏好上保存切换按钮的状态。但它不起作用。切换活动或关闭应用程序并重新打开时,切换按钮始终关闭。我的代码有什么问题?它正在进行第一次测试,当我再次重新编译应用程序时,错误,

public class huongdan extends Activity implements CompoundButton.OnCheckedChangeListener{

Switch vt,mbf,vnp,vnm,gmb;
TextView vtmb;

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

    // hien thi man hinh float
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;
    getWindow().setLayout((int) (width*1),(int) (height*0.80));
    // endhien thi man hinh float

    //switch viettel
    vt = (Switch) findViewById(R.id.switchvt);
    vt.setChecked(getFromSP("sw1"));
    vt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked == true){
                OneSignal.sendTag("viettel", "viettel");
            }
            else
                OneSignal.deleteTag("viettel");
        }
    });


    //switch mobi
    mbf = (Switch) findViewById(R.id.switchmbf);
    mbf.setChecked(getFromSP("sw2"));
    mbf.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked == true){
                OneSignal.sendTag("mobifone", "mobifone");
            }
            else
                OneSignal.deleteTag("mobifone");
        }
    });



    //switch vina
    vnp = (Switch) findViewById(R.id.switchvnp);
    vnp.setChecked(getFromSP("sw3"));
    vnp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked == true){
                OneSignal.sendTag("vinaphone", "vinaphone");
            }
            else
                OneSignal.deleteTag("vinaphone");
        }
    });


    //switch vietnammobile
    vnm = (Switch) findViewById(R.id.switchvnm);
    vnm.setChecked(getFromSP("sw4"));
    vnm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked == true){
                OneSignal.sendTag("vietnammobile", "vietnammobile");
            }
            else
                OneSignal.deleteTag("vietnammobile");
        }
    });

    //switch gmobile
    gmb = (Switch) findViewById(R.id.switchgmb);
    gmb.setChecked(getFromSP("sw5"));
    gmb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked == true){
                OneSignal.sendTag("gmobile", "gmobile");
            }
            else
                OneSignal.deleteTag("gmobile");
        }
    });

}
private boolean getFromSP(String key){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.sopu89.napthenoti", android.content.Context.MODE_PRIVATE);
    return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("com.sopu89.napthenoti", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch(buttonView.getId()){
        case R.id.switchvt:
            saveInSp("sw1",isChecked);
            break;
        case R.id.switchmbf:
            saveInSp("sw2",isChecked);
            break;
        case R.id.switchvnp:
            saveInSp("sw3",isChecked);
            break;
        case R.id.switchvnm:
            saveInSp("sw4",isChecked);
            break;
        case R.id.switchgmb:
            saveInSp("sw5",isChecked);
            break;
    }
}

}

2 个答案:

答案 0 :(得分:1)

最后的onCheckedChanged方法永远不会被调用,因此你的状态不会被保存。

原因是,您要为每个开关声明一个新的侦听器。 致电

saveinSp("sw1", isChecked);
在交换机1的监听器中

或将您的类设置为交换机的监听器并处理所有更改:

switch.setOnCheckedChangedListener(this);

答案 1 :(得分:0)

您需要从共享首选项中检索值,然后在onCreate方法

中设置按钮状态
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean tgpref = preferences.getBoolean("tgpref", true);  //default is true
if (tgpref = true) //if (tgpref) may be enough, not sure
{
  tg.setChecked(true);
}
else
{
  tg.setChecked(false);
}