【问题标题】:RadioButton is leaving black checked circle after uncheckingRadioButton 在取消选中后留下黑色选中的圆圈
【发布时间】:2020-08-27 12:08:51
【问题描述】:

我的 RadioGroup 中的 RadioButton 在我取消选中它们或单击组中的另一个 RadioButton 后留下一个黑色的选中圆圈。如何防止这种情况发生?

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test"
            android:textAlignment="textStart"
            android:layoutDirection="rtl"
            android:layout_gravity="start"
            android:background="?android:selectableItemBackground"/>

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="textStart"
            android:layoutDirection="rtl"
            android:layout_gravity="start"
            android:background="?android:selectableItemBackground"/>

</RadioGroup>

发生在我的 API 19 真实设备上,而不是我的 API 27

编辑:_________________________________________________

尝试使用不起作用的自定义选择器

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/qrmenu_toolbar"
        android:orientation="vertical">

<RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Resume"
            android:layoutDirection="rtl"
            android:layout_gravity="start"
            android:drawablePadding="12dp"
            android:paddingStart="16dp"
            android:paddingTop="12dp"
            android:paddingEnd="16dp"
            android:paddingBottom="12dp"
            app:drawableLeftCompat="@drawable/ic_resume"
            android:button="@drawable/radiobutton_selector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/radio_checked" android:state_checked="true" />
    <item android:drawable="@drawable/radio_unchecked" android:state_checked="false" />
</selector>

主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorBlack</item>
        <item name="colorPrimaryDark">@color/colorBlack</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

bump还是找不到解决办法

编辑:______________________________________

也尝试使用自定义单选按钮..仍然不起作用:

<RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:button="@drawable/custom_radio_button"/>

自定义单选按钮:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/ic_radio_button_checked"/>
    <item android:state_checked="false" android:drawable="@drawable/ic_radiobutton_unchecked"/>
</selector>

【问题讨论】:

  • 您是否尝试从 RadioButton 中删除 android:background 属性并对其进行测试?请提供设备型号。
  • 是的,我删除了背景属性,它仍然有它。三星 Galaxy Note3。
  • 我认为您可以尝试一些手动清除。通过 setOnCheckedChangeListener 将监听器添加到 RadioGroup 中,首先通过 clearCheck 清除当前检查,然后立即通过 check 方法设置检查项。
  • 尝试为每个RadioButton 提供ID,例如android:id="@+id/one"android:id="@+id/two"
  • 检查这个:- 这将有助于stackoverflow.com/a/34513545/9523118

标签: android android-radiobutton android-radiogroup


【解决方案1】:

从单选按钮中删除背景

android:background="?android:selectableItemBackground"

并使用 android:button="@drawable/radiobutton_selector"

以下是 Android 中自定义单选按钮的示例。请调查一下。 也许这对你有帮助。 http://www.apnatutorials.com/android/android-radiobutton-customization-and-usage.php?categoryId=2&subCategoryId=62&myPath=android/android-radiobutton-customization-and-usage.php

【讨论】:

  • 嘿戴夫,你试过这个吗?这对我有用。
【解决方案2】:

使用支持库(com.google.android.material)中的MaterialRadioButton。这个问题可能是因为RadioButton 的不同实现取决于API 版本以及主题和选择器。 MaterialRadioButton 的使用将统一 API 版本的行为。

Here 是这个组件的一个小礼物。

'com.google.android.material:material:1.0.0' 依赖项应导入项目和应用程序使用的 Material 主题之一。

希望它会有所帮助。

【讨论】:

  • 能否请您使用选择器、主题和 java 代码的代码扩展您的问题。使用 Button 属性而不是背景。
  • 没有Java代码只有XML,上面添加了选择器和主题。
【解决方案3】:

您必须为此使用 buttonTint。

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/radioButton"
    android:buttonTint="@color/your_color"/>

您的 radioButton 应该是这样的。但是,这在 api 21 之前不起作用 API 21 之前的设备有一个解决方案 Change Circle color of radio button

【讨论】:

    猜你喜欢
    • 2016-09-10
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2012-07-15
    相关资源
    最近更新 更多