英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论