我正在尝试保存Switch的状态。我已经检查了stackoverflow上的其他答案,但仍然没有想出来。
我的主要活动是切换按钮(tgbutton)和开关(simpleSwitch1)。切换按钮的首选项有效但开关没有。不确定我是否正确保存了切换按钮的首选项,但对于大多数其他论坛/答案,他们已经这样做了。
请指教,
public class MainActivity extends AppCompatActivity {
Switch simpleSwitch1;
boolean switchState2 = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate view's
simpleSwitch1 = (Switch) findViewById(R.id.simpleSwitch1);
SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyle", MODE_PRIVATE);
simpleSwitch1.setChecked(sharedPrefs.getBoolean("NameOfThingToSave",false));
switchState2 = sharedPrefs.getBoolean("NameOfThingToSave", false);
final ToggleButton tgbutton;
tgbutton = (ToggleButton) findViewById(R.id.toggle);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
tgbutton.setChecked(sharedPreferences.getBoolean("toggleButton", false)); //default is false
tgbutton.setOnClickListener(new ToggleButton.OnClickListener() {
public void onClick(View v) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("toggleButton", tgbutton.isChecked());
editor.commit();
}
});
simpleSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.commit();
Toast.makeText(MainActivity.this, "This is on " + switchState2, Toast.LENGTH_SHORT).show();
} else {
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", false);
editor.commit();
Toast.makeText(MainActivity.this, "This is off " + switchState2, Toast.LENGTH_SHORT).show();
}
}
});
}
}
答案 0 :(得分:1)
每次存储/检索不必要的值时,您都会声明新的SharedPreferences
。只需使用一个SharedPreferences
并使用不同的键跟踪不同的值。您应该将您的值存储在onStop()
中,因为这更有意义。请尝试以下代码:
public class MainActivity extends AppCompatActivity {
Switch simpleSwitch1;
boolean switchState2 = false;
SharedPreferences sharedPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate view's
simpleSwitch1 = (Switch) findViewById(R.id.simpleSwitch1);
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(sharedPrefs.contains("NameOfThingToSave")){
simpleSwitch1.setChecked(sharedPrefs.getBoolean("NameOfThingToSave",false));
switchState2 = sharedPrefs.getBoolean("NameOfThingToSave", false);
}
final ToggleButton tgbutton;
tgbutton = (ToggleButton) findViewById(R.id.toggle);
if(sharedPrefs.contains("toggleVutton")){
tgbutton.setChecked(sharedPrefs.getBoolean("toggleButton", false)); //default is false
}
tgbutton.setOnClickListener(new ToggleButton.OnClickListener() {
public void onClick(View v) {
}
});
simpleSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
}
@Override
public void onStop() {
// Store toggle state
sharedPrefs.edit().putBoolean("toggleButton", tgbutton.isChecked()).commit();
// Store switch state
if (simpleSwitch1.isChecked){
sharedPrefs.edit().putBoolean("NameOfThingToSave", true).commit();
Toast.makeText(MainActivity.this, "This is on " + switchState2, Toast.LENGTH_SHORT).show();
} else {
sharedPrefs.edit().putBoolean("NameOfThingToSave", false).commit();
Toast.makeText(MainActivity.this, "This is off " + switchState2, Toast.LENGTH_SHORT).show();
}
}
}
希望这有帮助。
答案 1 :(得分:0)
问题是,您使用different
首选项("com.example.xyle"
和"com.example.xyz"
)来存储并获得boolean
值。
switchState2
的值在您的代码中的任何位置都没有改变,这就是为什么在Toast
消息中它始终显示相同的结果。
试试这个:
public class MainActivity extends AppCompatActivity {
Switch simpleSwitch1;
boolean switchState2 = false;
SharedPreferences sharedPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate view's
simpleSwitch1 = (Switch) findViewById(R.id.simpleSwitch1);
sharedPrefs = getSharedPreferences("com.example.xyz", MODE_PRIVATE);
simpleSwitch1.setChecked(sharedPrefs.getBoolean("NameOfThingToSave",false));
switchState2 = sharedPrefs.getBoolean("NameOfThingToSave", false);
final ToggleButton tgbutton = (ToggleButton) findViewById(R.id.toggle);
tgbutton.setChecked(sharedPrefs.getBoolean("toggleButton", false)); //default is false
tgbutton.setOnClickListener(new ToggleButton.OnClickListener() {
public void onClick(View v) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("toggleButton", tgbutton.isChecked());
editor.commit();
}
});
simpleSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("NameOfThingToSave", true);
editor.commit();
switchState2 = true;
Toast.makeText(MainActivity.this, "This is on " + switchState2, Toast.LENGTH_SHORT).show();
} else {
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", false);
editor.commit();
switchState2 = false;
Toast.makeText(MainActivity.this, "This is off " + switchState2, Toast.LENGTH_SHORT).show();
}
}
});
}