热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用复选框来确定显示或跳过入门屏幕?-Howtousecheckboxtodetermineshoworskiponboardingscreen?

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.

如果你写我如何让入门画面不再显示,如果用户选中复选框放入入门画面活动,我将不胜感激。

Onboarding screen

1 个解决方案

#1


0  

Change code as below

更改代码如下

SharedPreferences preferences = PreferenceManager.getSharedPreferences("PREFERENCE");
        final SharedPreferences.Editor editor = preferences.edit();

radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            editor.putBoolean("isShowOnboarding", false);
            editor.commit();

        }else {
            editor.putBoolean("isShowOnboarding", true);
            editor.commit();
        }
        }
    }

Add this to main activity

将其添加到主要活动

Boolean isShowOnboarding= getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isShowOnboarding", true);

    if (isShowOnboarding) {
        //show sign up activity
        startActivity(new Intent(MainActivity.this, Preshow.class));
        finish();
    }

推荐阅读
author-avatar
大耍酷的微博Katharine
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有