SharedPreference以记住复选框是可见的并已选中



我有一个不可见的复选框,但当我单击菜单项时,复选框是可见的。如果复选框被选中,如果我离开应用程序并返回应用程序以保持复选框可见,SharedPreferences应该是什么样子。现在,当我离开应用程序并选中复选框时,当我返回应用程序时,复选框是不可见的,并且不记得复选框是选中的。

<CheckBox
android:id="@+id/my_check"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Save Audio file"
android:textColor="#000000"
android:layout_marginTop="20dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

private CheckBox my_check;
// onCreate
addListenerOnSaveAudio();

switch (item.getItemId()) {
case R.id.save_audio:
saveAudio.setVisibility(View.VISIBLE);
}

public void addListenerOnSaveAudio() {
my_check = (CheckBox) findViewById(R.id.my_check);
my_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}

这是sharedPreference的文档以及如何使用它。当有人点击你的菜单时,你需要保存复选框的状态,你可以使用:

// in onCreate
Context context = getActivity();
SharedPreferences preferences = context.getSharedPreferences(
"NAME OF YOUR CHOICE", Context.MODE_PRIVATE);
CheckBox checkBox = findViewById(R.id.my_check);
SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("my_check") && preferences.getBoolean("my_check",false) == true) {
checkBox.setChecked(true);
}else {
checkBox.setChecked(false);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox.isChecked()) {
editor.putBoolean("my_check", true);
editor.apply();
}else{
editor.putBoolean("my_check", false);
editor.apply();
}
}
}); 
private boolean state;
private SharedPreferences sharedPreferences;
private Checkbox my_check;
public void addListenerOnSaveAudio() {
//Instance for save state of my_check
sharedPreferences = getSharedPreferences("name_preferences",Context.MODE_PRIVATE);
my_check = (CheckBox) findViewById(R.id.my_check);
// If my check not exist by default is false else true
state = sharedPreferences.getBoolean("my_check", false);
my_check.setChecked(state);
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//When do click in my_check will change state in preferences and when open your app is apply changes in view.
sharedPreferences.edit()
.putBoolean("my_check",isChecked);
if (isChecked) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}

您可以使用onCheckedChanged()将侦听器注册到checkBox,然后将其检查状态保存到共享首选项中。

并在每次启动应用程序时获得共享的偏好值,即onCreate()方法

因此,将您的活动的onCreate()addListenerOnSaveAudio()修改为以下

public class MyActivity extends AppCompatActivity implements View.OnClickListener {
private CheckBox my_check;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
my_check = findViewById(R.id.my_check);
SharedPreferences sharedPrefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
boolean isChecked = sharedPrefs.getBoolean("checkValue", false); // default value is false
my_check.setChecked(isChecked);

addListenerOnSaveAudio();

}

public void addListenerOnSaveAudio() {
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("checkValue", isChecked);
editor.apply();
if (!isChecked)
saveAudio.setVisibility(View.INVISIBLE);
}
});
}
}

相关内容

  • 没有找到相关文章

最新更新