问题描述
我有一个项目,我想在共享首选项上保存开关按钮的状态.但它不起作用.当我切换活动或关闭应用程序并重新打开时,切换按钮总是关闭.我的代码有什么问题?它在第一次测试中工作,当我再次重新构建应用程序时,它出错了,
I have a project, i want to save state of switch button on sharedpreferences. but it not working. Switch button always off when i switch activity or close app and reopen. what wrong in my code ? It working on first test, when i rebuid app again it wrong,
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;
}
}
}
推荐答案
最后的 onCheckedChanged 方法将永远不会被调用,因此您的状态不会被保存.
the onCheckedChanged-method in the end will never be called and therefore your states are not going to be saved.
这样做的原因是,您要为每个开关声明一个新的侦听器.打电话
The reason for this is, that you are declaring a new listener for every switch.call
saveinSp("sw1", isChecked);
在第一个开关的监听器中或将您的类设置为开关的监听器并在那里处理所有更改:
in the listener of switch one or set your class as listener for the switches and handle all changes there:
switch.setOnCheckedChangedListener(this);
这篇关于与开关按钮共享偏好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!