保持 Android 应用中开关的选中状态。

huangapple go评论72阅读模式
英文:

Keep switch checked in Android application

问题

以下是您提供的代码部分的翻译:

mWorkingSwitch = (Switch) findViewById(R.id.workingSwitch);
SharedPreferences prefs = getSharedPreferences("com.bitproject.driverapplication", MODE_PRIVATE);
boolean switchState = prefs.getBoolean("service_status", false);
mWorkingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if (isChecked) {
             connectDriver();
         } else {
             disconnectDriver();
         }
     }
});
mWorkingSwitch.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         SharedPreferences.Editor editor = getSharedPreferences("com.bitproject.driverapplication", MODE_PRIVATE).edit();
         editor.putBoolean("service_status", mWorkingSwitch.isChecked());
         editor.commit();
     }
});

当我返回(开关处于选中状态)并再次进入该活动(开关所在的活动),该开关已经变为关闭状态。

我想保持开关打开,直到我将其关闭。
虽然有一些类似的问题,但我不知道如何将它们与我的问题联系起来。我对Android不太熟悉。

英文:

I have a switch in my Android app. I want to keep that checked until I off it (even I back pressed or open another application). Below is the code that I have tried.

mWorkingSwitch = (Switch) findViewById(R.id.workingSwitch);
SharedPreferences prefs = getSharedPreferences("com.bitproject.driverapplication", MODE_PRIVATE);
boolean switchState = prefs.getBoolean("service_status", false);
mWorkingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if (isChecked) {
             connectDriver();
         } else {
             disconnectDriver();
         }
     }
});
mWorkingSwitch.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         SharedPreferences.Editor editor = getSharedPreferences("com.bitproject.driverapplication", MODE_PRIVATE).edit();
         editor.putBoolean("service_status", mWorkingSwitch.isChecked());
         editor.commit();
     }
});

When I backpressed (while switch is checked), and again go to that activity (where the switch is in), that switch has become off.

I want to keep it on until I off it.
There are some similar questions but I can't understand how to relate those to my problem. I'm new to Android.

答案1

得分: 1

setOnCheckedChangeListener中保存状态到共享偏好:

......
......
if (isChecked) {
    connectDriver();

    // 开关被选中
    SharedPreferences.Editor editor = getSharedPreferences("switch", MODE_PRIVATE).edit();
    editor.putString("state", "checked");
    editor.apply();

} else {
    disconnectDriver();

    // 开关未选中
    SharedPreferences.Editor editor = getSharedPreferences("switch", MODE_PRIVATE).edit();
    editor.putString("state", "unchecked");
    editor.apply();
}
......
......

现在在活动的onStart()方法中:

@Override
protected void onStart() {
    super.onStart();

    // 读取状态
    SharedPreferences pref = getSharedPreferences("switch", MODE_PRIVATE);
    String state = pref.getString("state", "default");

    if (state.equals("checked")) {

        // 它之前被选中
        mWorkingSwitch.setChecked(true);

    } else {

        // 它之前未选中
        mWorkingSwitch.setChecked(false);

    }
}
英文:

Save state in shared preferences in setOnCheckedChangeListener:

......
......               
if (isChecked) {
connectDriver();

//switch is checked
SharedPreferences.Editor editor = getSharedPreferences("switch", MODE_PRIVATE).edit();
editor.putString("state", "checked");
editor.apply();

} else {
disconnectDriver();

//switch is unchecked
SharedPreferences.Editor editor = getSharedPreferences("switch", MODE_PRIVATE).edit();
editor.putString("state", "unchecked");
editor.apply();
}
......
...... 

Now in onStart() method of activity:

@Override
protected void onStart(){
super.onStart();

//read the state
SharedPreferences pref = getSharedPreferences("switch", MODE_PRIVATE); 
String state = pref.getString("state", "default");

if(state.equals("checked")){

//it was checked

mWorkingSwitch.setChecked(true);

}else{

//it was unchecked

mWorkingSwitch.setChecked(false);

}

}

答案2

得分: 1

你还应该在onCreate方法中,在switchState被赋值后,恢复其状态:

mWorkingSwitch.setChecked(switchState);
英文:

You should also restore its state also in the onCreate method after switchState is assigned:

mWorkingSwitch.setChecked(switchState);

答案3

得分: 1

好的,这是翻译好的部分:

Ok这是修复后的代码您应该在OnCheckedChangeListener中保存状态并使用`mWorkingSwitch.setChecked(switchState);`来恢复状态

mWorkingSwitch = (Switch) findViewById(R.id.workingSwitch);
SharedPreferences prefs = getSharedPreferences("com.bitproject.driverapplication", MODE_PRIVATE);
boolean switchState = prefs.getBoolean("service_status", false);
mWorkingSwitch.setChecked(switchState);
mWorkingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if (isChecked) {
             connectDriver();
         } else {
             disconnectDriver();
         }

 SharedPreferences.Editor editor = getSharedPreferences("com.bitproject.driverapplication", MODE_PRIVATE).edit();
         editor.putBoolean("service_status", isChecked);
         editor.commit();
     }
});

您不应该为开关设置onClickListener
英文:

Ok here is the fixed code , you should be saving state in on OnCheckedChangeListener and setting state back with mWorkingSwitch.setChecked(switchState);

mWorkingSwitch = (Switch) findViewById(R.id.workingSwitch);
SharedPreferences prefs = getSharedPreferences("com.bitproject.driverapplication", MODE_PRIVATE);
boolean switchState = prefs.getBoolean("service_status", false);
 mWorkingSwitch.setChecked(switchState);
mWorkingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if (isChecked) {
             connectDriver();
         } else {
             disconnectDriver();
         }

 SharedPreferences.Editor editor = getSharedPreferences("com.bitproject.driverapplication", MODE_PRIVATE).edit();
         editor.putBoolean("service_status",isChecked);
         editor.commit();
     }
});

You should not be setting onClickListener for switch

huangapple
  • 本文由 发表于 2020年5月4日 00:44:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61578154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定