【问题标题】:Radio Button is only partially checked单选按钮仅被部分选中
【发布时间】:2018-02-06 03:42:23
【问题描述】:

我知道这听起来很奇怪,所以这是图片。

它保持正确的值。 (部分)选择了正确的单选按钮。 OnCheckedChangeListener 中的所有逻辑都正确执行。我完全惊呆了。为什么单选按钮没有完全选中?

我唯一能想到的是我正在使用Rx2Firebase

periodRetriever = FirebaseHelper.getInstance(getContext()).getPeriod()
        .defaultIfEmpty(0)
        .distinctUntilChanged()
        .subscribe(new Consumer<Integer>() {
            @Override
            public void accept(@NonNull Integer headerType) throws Exception {
                getRadioViewChecked(headerType).setChecked(true);
            }
        });

EDIT1

Marcos 建议我看不到白色的勾号。不是这种情况。

EDIT2

布局:

<RadioGroup
    android:id="@+id/rgPeriod"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rbMonth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/month" />

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rbWeek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/week" />

    <android.support.v7.widget.AppCompatRadioButton
        android:id="@+id/rb4Weeks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="@string/four_weeks" />


</RadioGroup>

【问题讨论】:

  • 你指的是圆的中心没有圆点吗?如果是这样,您的主题是什么,您是否对RadioButton 的外观进行了自定义?
  • @CommonsWare 是的,这就是我所指的!除了设置colorPrimarycolorAccent 之外,我没有对样式做任何事情。全部同步的时候我没有这个问题。
  • 也许你扩展的主题使用白色选择点如果你使用深色主题切换到浅色,否则反之
  • 当我手动选择它时,它工作正常。事件第二次触发时也是如此。
  • 您是否尝试在主线程上使用.observeOn(AndroidSchedulers.mainThread()).subscribe(...) 处理它?

标签: android android-layout android-radiogroup android-radiobutton


【解决方案1】:

看起来像一个动画错误。在 ViewPager 内的 Fragment 上使用 CompoundButton 时,drawable 状态设置不正确。

复选框也会出现这种情况,如下所示:Android Nougat: Why do checkboxes on Fragment have incomplete state when selected programmatically (but look fine on Lollipop)

在调用 RadioGroup.check(id) 或 Checkbox.setChecked(true) 之后调用 jumpDrawablesToCurrentState() 似乎可以解决问题(谢谢@Dalmas)

【讨论】:

  • 在所有这些答案之后,终于有道理了。
  • 这发生在 Nexus 5x API 27 上。片段位于带有片段适配器的活动中。 #jumpDrawablesToCurrentState 修复了它
  • 对我来说 jumpDrawablesToCurrentState 解决了这个问题。谢谢!
  • radioButton.jumpDrawablesToCurrentState();为我工作
【解决方案2】:

您的主题是深色,看不到白色勾号,您需要将其更改为另一种颜色。

【讨论】:

  • Obs:我确实在 Aug28 发表了评论
  • 如果这是真的,改变我的活动的背景颜色会显示这一点吗?但接下来我想到了一个新问题,为什么它有时是白色的,有时不是?
  • 这取决于您正在测试的设备,是的,改变背景会使它出现
  • 那为什么有时是白色有时不是呢?虽然是同一设备
  • 所以创建一个最小的可重现代码以便我们进行测试
【解决方案3】:

除了@Nipper 的出色回答,我还在我的项目中写了一个 Kotlin 扩展来轻松解决这个问题:

fun CompoundButton.setCheckedFixed(checked: Boolean) {
    isChecked = checked
    jumpDrawablesToCurrentState()
}

我应该在任何地方使用 radioButton.isChecked = trueradioButton.setChecked(true) 而不是 radioButton.setCheckedFixed(true)

【讨论】:

    【解决方案4】:

    尝试更改您的主题或单选按钮的颜色属性

    【讨论】:

      【解决方案5】:

      将背景颜色从白色更改为它会出现的另一种颜色

      【讨论】:

      • 你怎么会错过那张红色的照片?
      • 抱歉 Robin Dijkhof 先生
      • 我认为他正在应用一些主题
      【解决方案6】:

      您是否尝试过使用RadioButton 而不是android.support.v7.widget.AppCompatRadioButton 我认为这可能会解决问题

      另一方面,您可以在此处找到有关如何设置 RadioButtons 样式的文档

      http://www.materialdoc.com/radio-button/

      1. 在您的 styles.xml 文件中声明自定义样式。

      <style name="MyRadioButton" parent="Theme.AppCompat.Light"> <item name="colorControlNormal">@color/indigo</item> <item name="colorControlActivated">@color/pink</item> </style>

      1. 通过 android:theme 属性将此样式应用于您的 RadioButton。

      `

         <RadioButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:checked="true"
              android:text="Radio Button"
              android:theme="@style/MyRadioButton"/>
      

      来源:https://stackoverflow.com/a/35044856/4492504

      【讨论】:

        猜你喜欢
        • 2016-06-09
        • 1970-01-01
        • 2023-02-03
        • 2013-10-02
        • 2013-07-27
        • 1970-01-01
        • 2019-12-21
        • 2013-06-18
        • 1970-01-01
        相关资源
        最近更新 更多