【问题标题】:How to use checkbox to determine show or skip onboarding screen?如何使用复选框来确定显示或跳过入职屏幕?
【发布时间】:2019-01-17 01:42:17
【问题描述】:

我有一个问题,我不知道如何保存复选框状态并在共享首选项中使用它来显示或跳过我的应用程序的入职(演练)屏幕。我只在应用程序首次启动时制作了显示入职屏幕,但这不是我想要的。 我需要使用复选框来允许用户选择是否要在启动时再次显示它。

这是我的入门屏幕布局和课程。

<CheckBox
    android:id="@+id/radiobtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/prevBtn"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="40dp"
    android:buttonTint="@color/white"
    android:checked="false"
    android:text="do not show again"
    android:textColor="@color/white"
    android:textSize="12sp" />

    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();
            }
            }
        }

我知道这可能没有意义,因为我自己在尝试。我没有看到任何可以练习的例子。

这是我在第二次发布时跳过入职屏幕的共同偏好。此代码属于 MainActivity,在 onboarding 活动后显示。

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();

如果您给我写信,如果用户选中放置在入职屏幕活动中的复选框,如何使入职屏幕不再显示,我将不胜感激。

【问题讨论】:

  • 只需检查键和值是否实际保存。为我检查提交,这似乎是罪魁祸首。在 editor.putBoolean("checked", true); 之后我没有看到任何提交;
  • 勾选复选框后是否按下任何按钮继续?
  • @mTak ye,ZAKONCZ 按钮,代表完成。然后导航到 MainActivity - 应用程序的核心。

标签: android checkbox sharedpreferences


【解决方案1】:

修改代码如下

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();
        }
        }
    }

将此添加到主要活动中

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

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

【讨论】:

  • 我已经改用 editor.apply 了,还是没有效果。也许我阅读了复选框的状态并将其保存到首选项,但是当未选中复选框时,我无法部署功能以启动入职屏幕活动。我应该在哪里添加 startActivity(new Intent(MainActivity.this, Preshow.class));完成();?
  • 打开应用时加载的第一个活动是什么?
  • 现在是 MainActivity,它是应用程序的核心。但是只要在 MainActivity 中启动 OnCreate,我就会检查应用程序是否是第一次使用 sharedpreferences 启动。如果 if (isFirstRun) { startActivity(new Intent(MainActivity.this, Preshow.class));结束();显示入职。现在,当我删除它时,它会一直启动 MainActivity。
  • 即使我选中复选框,更新代码也会使入职屏幕出现不间断。这样我就无法进入 MainActivity。通过单击 ZAKONCZ 按钮,它应该显示 MainActivity 并记住代表 onboardingscreen 的复选框的选择。这是按钮 mEndBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(),MainActivity.class); startActivity(intent); finish(); }}); 的代码
  • 请分享您的项目。我无法理解你真正需要做什么
猜你喜欢
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2019-05-01
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
相关资源
最近更新 更多