【问题标题】:Three CheckBoxes in a settings screen only letting the user click one at a time设置屏幕中的三个复选框只允许用户一次单击一个
【发布时间】:2015-12-20 23:48:51
【问题描述】:

我在设置屏幕中有三个复选框,但我只希望允许用户单击一个。我怎样才能做到这一点,当用户单击一个时,它会取消阻止另一个。这是我的主要活动中的代码,它将复选框连接到它们的声音值。

            standardSound = MediaPlayer.create(this, R.raw.fanpost);

            alternateSound = MediaPlayer.create(this, R.raw.alternate_fan);

            whiteSound = MediaPlayer.create(this, R.raw.white_noise);

            SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

            boolean alternate = getPrefs.getBoolean("alternate", false);
            boolean white = getPrefs.getBoolean("white", false);
            boolean standard = getPrefs.getBoolean("standard",false);

            if (blade.getAnimation() == null) {


                // no animation, start it
                if (alternate == true) {
                    alternateSound.start();
                    alternateSound.setLooping(true);
                    blade.startAnimation(animRotate);
                    if (blade.getAnimation() == null) {
                        alternateSound.stop();
                        alternateSound.prepareAsync();
                    }


                } else if (white == true) {
                    whiteSound.start();
                    whiteSound.setLooping(true);
                    blade.startAnimation(animRotate);
                    if (blade.getAnimation() == null) {
                        whiteSound.stop();
                        whiteSound.prepareAsync();
                    }

                } else if (standardFan == true) {
                    standardSound.start();
                    standardSound.setLooping(true);
                    blade.startAnimation(animRotate);
                    if (blade.getAnimation() == null) {
                        standardSound.stop();
                        standardSound.prepareAsync();
                    }

                }

【问题讨论】:

  • 单选按钮就是为了做到这一点。
  • 复选框代码部分在哪里?
  • sharedPreference 布尔值是复选框
  • 那么你将如何使用单选按钮来处理这类事情
  • 您希望用户只选择一个框还是允许从第一个选择中访问其他选项?

标签: android checkbox sharedpreferences


【解决方案1】:

解决方案是在您的偏好 xml 文件中使用ListPreference,这是一个教程: ListPreference

【讨论】:

  • 这行不通。我无法在单独的首选项屏幕中将 onClickListener 设置为复选框,而且您从未定义过 isChecked 是什么
  • isChecked 是 CheckBox 类的一个方法。单独的偏好屏幕是什么意思?
  • 这段代码在我的主要活动中,它从我的偏好屏幕中获取值。
  • 好吧,我想我可以在我的偏好类中执行此代码,但我将如何引用复选框。
  • 好的,我得到你想要做的,你只需要使用 ListPreference。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 2017-02-26
  • 2012-10-27
  • 1970-01-01
  • 2016-05-22
相关资源
最近更新 更多