作者:大耍酷的微博Katharine | 来源:互联网 | 2023-09-09 16:13
IvegotaissuethatIdonthaveacluehowtosavecheckboxstateanduseitinsharedpreferences
I've got a issue that I don't have a clue how to save checkbox state and use it in sharedpreferences to display or skip onboarding (walkthrough) screen to my application. I have made to make display onboarding screen only at first launch of the app, but that's not what I'm looking for. I need to use checkbox to allow user choose if they want to display it again at launch or not.
我有一个问题,我不知道如何保存复选框状态并在共享偏好中使用它来显示或跳过入门(演练)屏幕到我的应用程序。我只是在第一次启动应用程序时制作了显示器入门屏幕,但这不是我想要的。我需要使用复选框来允许用户选择是否要在启动时再次显示它。
Here's my onboarding screen layout and class.
这是我的入职屏幕布局和课程。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("checked") && preferences.getBoolean("checked", false) == true){
radioButton.setChecked(true);
}else{
radioButton.setChecked(false);
}
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(radioButton.isChecked()){
editor.putBoolean("checked", true);
editor.apply();
}else {
editor.putBoolean("checked", false);
editor.apply();
}
}
}
I know It might have no sense cus I'm trying on my own. I don;t see any example to practice.
我知道我可能没有任何意义,我正在尝试自己。我没有看到任何练习的例子。
Here's my sharedpreferences to skip onboarding screen at second launch. This code belongs to MainActivity which is displayed after onboarding activity.
这是我在第二次发布时跳过入门屏幕的共享偏好。此代码属于MainActivity,在活动后显示。
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
//show sign up activity
startActivity(new Intent(MainActivity.this, Preshow.class));
finish();
}
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).commit();
I'll be appreciated if you write me how to make onboarding screen to not show again if user checks checkbox which is placed in onboarding screen activity.
如果你写我如何让入门画面不再显示,如果用户选中复选框放入入门画面活动,我将不胜感激。
1 个解决方案