[英]Shared Preference saving the checkbox action
I was able to store the value of checkbox using shared preference but I was not able to save the action its meant to do.The action what i need is when checkbox is checked a button should display,if checkbox is unchecked button should not display(hiding/showing button is done in different activity). 我能够使用共享偏好来存储复选框的值,但我没能拯救行动它的意思do.The行动我需要的是当复选框被选中一个按钮将显示,如果复选框未被选中按钮不应显示(隐藏/显示按钮是在其他活动中完成的)。 So what I did was i passed the value
isCheckedValue = isChecked;
所以我所做的就是我传递了值
isCheckedValue = isChecked;
under if/else condition 在if / else条件下
final CheckBox checkBox = (CheckBox) findViewById(R.id.add_fb);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
checkBox.setChecked(preferences.getBoolean("checked",false));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isCheckedValue = isChecked;
editor.putBoolean("checked", isChecked);
editor.apply();
if(checkBox.isChecked()) {
isCheckedValue = isChecked;
editor.putBoolean("checked", true);
editor.apply();
}else{
editor.putBoolean("checked", false);
editor.apply();
}
}
});
if chkbox is checked the value will pass using intent in onBubbleClick
using the boolean passing data in.putExtra("yourBoolName", isCheckedValue );
如果选中了chkbox,则该值将在
onBubbleClick
使用intent传递, onBubbleClick
使用in.putExtra("yourBoolName", isCheckedValue );
的布尔值传递数据in.putExtra("yourBoolName", isCheckedValue );
you could notice it in below bunch of code 您可以在下面的一堆代码中注意到它
private void addNewBubble() {
BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
@Override
public void onBubbleRemoved(BubbleLayout bubble) {
finish();
System.exit(0);
}
});
bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {
@Override
public void onBubbleClick(BubbleLayout bubble) {
Intent in = new Intent(MainActivity.this, PopUpWindow.class);
in.putExtra("yourBoolName", isCheckedValue );
startActivity(in);
}
});
bubbleView.setShouldStickToWall(true);
bubblesManager.addBubble(bubbleView, 60, 20);
}
How it works: At the very beginning the button is not displayed until the checkbox is clicked, the button gets displayed once the checkbox is checked and never gets hidden even after unchecked the checkbox. 工作原理:一开始,直到单击该复选框,该按钮才会显示;一旦选中该复选框,该按钮就会显示出来,即使取消选中该复选框也不会隐藏。
How it's meant to work The button should display if the checkbox is checked and button should hide if the checkbox is unchecked. 工作原理如果已选中复选框,则应显示按钮;如果未选中此复选框,则应隐藏按钮。
In your if-else block, you aren't really updating the value of isCheckedValue
in false
condition. 在if-else块中,您实际上并没有在
false
条件下更新isCheckedValue
的值。 So, that needs to be fixed. 因此,这需要解决。 I also refactored your code a bit.
我也重构了您的代码。 Try the following:
请尝试以下操作:
final CheckBox checkBox = (CheckBox) findViewById(R.id.add_fb);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
checkBox.setChecked(preferences.getBoolean("checked",false));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isCheckedValue = isChecked;
editor.putBoolean("checked", isChecked);
editor.apply();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.