gpt4 book ai didi

android - 带有切换按钮的共享首选项

转载 作者:行者123 更新时间:2023-11-30 01:18:39 26 4
gpt4 key购买 nike

我有一个项目,我想在 sharedpreferences 上保存开关按钮的状态。但它不起作用。当我切换 Activity 或关闭应用程序并重新打开时,切换按钮始终关闭。我的代码有什么问题?它在第一次测试时工作,当我再次重建应用程序时它出错了,

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;
}
}

最佳答案

您需要从共享首选项中检索值,然后在 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);
}

关于android - 带有切换按钮的共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37438192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com