单选按钮仅部分选中。

14

我知道这听起来很奇怪,所以这里有一张图片。

在这里输入图片描述

它保留了正确的值。正确的单选按钮被(部分)选中。所有 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);
            }
        });

编辑1

Marcos建议我看不到白色勾号,这不是事实。 输入图片描述

编辑2

布局:

<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>

你尝试过使用.observeOn(AndroidSchedulers.mainThread()).subscribe(...)在主线程中处理它吗? - Kirill
@g4s8 很遗憾,它没有帮助到。 - Robin Dijkhof
添加您的 XML 布局。 - Qamar
你是否正在动态设置RadioButton的可绘制对象? - Pablo Baxter
@PabloBaxter 只需颜色。 - Robin Dijkhof
显示剩余4条评论
6个回答

23

1
在所有这些答案之后,终于有了一些有意义的东西。 - Robin Dijkhof
这发生在 Nexus 5x API 27 上。该片段位于具有片段适配器的活动中。#jumpDrawablesToCurrentState 修复了它。 - Reinherd
1
对于我来说,jumpDrawablesToCurrentState解决了这个问题。谢谢! - Oya
radioButton.jumpDrawablesToCurrentState(); 对我有用。 - Ramesh kumar

0

你的主题是暗色调,无法看到白色的勾选标记,你需要将其更改为其他颜色。


1
我在8月28日对此进行了评论。 - Marcos Vasconcelos
如果是这样,更改我的activity的背景颜色会显露出来对吗?但接着我又有一个新问题,为什么有时是白色,有时不是呢? - Robin Dijkhof
嗯,也许是动画问题?你确定你的方法只检查了其中一个检查项吗? - Marcos Vasconcelos
绝对肯定。 - Robin Dijkhof
4
请提供一个最小可复现的代码,以便我们可以进行测试。 - Marcos Vasconcelos
显示剩余2条评论

0
除了@Nipper的优秀答案之外,我在我的项目中编写了一个Kotlin扩展来轻松解决这个问题:
fun CompoundButton.setCheckedFixed(checked: Boolean) {
    isChecked = checked
    jumpDrawablesToCurrentState()
}

每次我应该使用radioButton.isChecked = trueradioButton.setChecked(true)时,我却使用了radioButton.setCheckedFixed(true)


-1

将背景颜色从白色更改为其他颜色,它将会出现。


1
你怎么会错过那个红色的图像呢? - Robin Dijkhof
抱歉 Robin Dijkhof 先生。 - dileep krishnan
我认为他正在应用一些主题。 - dileep krishnan

-1

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


-1

你尝试过使用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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接