【问题标题】:Android I cannot uncheck initial RadioButtonAndroid 我无法取消选中初始 RadioButton
【发布时间】:2018-03-21 02:02:18
【问题描述】:

设置默认选中按钮后,我无法取消选中初始单选按钮。

问题情况 :: XML 代码

<RadioGroup
    android:id="@+id/stateRadioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <RadioButton
        android:id="@+id/attendanceRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="School"
        android:textSize="18sp" />

    <RadioButton
        android:id="@+id/hometimeRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Home"
        android:textSize="18sp" />

</RadioGroup>

问题情况 :: 初始状态 -> 运行良好

问题情况 :: 当我点击 Home 键时......

我不明白为什么会这样。我已经从 stackoverflow 阅读了this article。但这对我没有帮助,而且太旧了。

我试图解决的问题

在 XML 中删除 1 行

我删除线,android:checkedButton="@+id/attendanceRadioButton" 我工作得很好。 (我的意思是永远不会同时单击两个按钮)但是没有默认的选中按钮。所以这不是我想要的。

在 onCreateView() 中添加代码

我已经在onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 中添加了 2 行但没有帮助。 (其实这些view都是在Fragment上的。)

stateRadioGroup.clearCheck();
attendanceRadioButton.setChecked(true);

结果与上图相同。

我的 Android 工作室系统(配置)

Android Studio : 3.0.1
摇篮:4.1
compileSdkVersion : 27
minSdkVersion : 21
targetSdkVersion : 23
运行设备:LG V10、Android 7.0

==============

我想我找到了真正可能的原因

我创建了新项目来解决这个问题。我认为这个问题与 Fragment 有关系。但我不知道如何解决这个问题。所以,请帮助我:(

在这种情况下,发生了上情况。

MainActivityRadioButtonFragment,我将从MainActivity 扩充RadioButtonFragment 中的片段,如下代码所示。

activity_main.xml

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="Your Package Name">

    <fragment
        android:id="@+id/fragmentLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:name="io.github.tyeolrik.testradiobutton.RadioButtonFragment"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.add(R.id.fragmentLayout, new RadioButtonFragment());
    fragmentTransaction.commit();

    setContentView(R.layout.activity_main);
}

fragment_radio_button.xml

<!-- Of course there is ConstraintLayout which wraps RadioGroup -->
<RadioGroup
    android:id="@+id/buttonGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:checkedButton="@+id/button1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <RadioButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="School" />

    <RadioButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Home" />
</RadioGroup>

RadioButtonFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_radio_button, container, false);

    return view;
}

问题说明

当我在 Activity 中创建一个 RadioGroup 和一些 RadioButtons 时效果很好。但是,在那种情况下,在 Fragment 中制作 RadioButtons,就会出现两个按钮检查问题。

有没有办法解决这个问题?

我导入的内容。

不是android.support.v4.app.Fragment而是android.app.Fragment

在 MainActivity.java 中

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

在 RadioButtonFragment.java 中

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

【问题讨论】:

  • @ZarNiMyoSettWin 首先,当片段启动时,“学校”单选按钮被选中。因为,我不想制作空按钮。但问题是,当我切换“主页”按钮时,应该取消选中“学校”按钮,但它并没有如您所见。
  • 好的,你的意思是“学校”单选按钮在应用启动时默认选中?
  • @ZarNiMyoSettWin 是的!
  • 如果是这样,你只需要在你的单选按钮xml中添加android:checked="true",试试下面

标签: android radio-button radio-group


【解决方案1】:

试试这个, 从您的 onCreateView() 方法中删除以下代码

stateRadioGroup.clearCheck();
attendanceRadioButton.setChecked(true);

并在您的 xml 中添加以下内容。

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp">


            <RadioGroup
                android:id="@+id/stateRadioGroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <RadioButton
                    android:id="@+id/attendanceRadioButton"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="School" />

                <RadioButton
                    android:id="@+id/hometimeRadioButton"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Home" />

            </RadioGroup>
        </LinearLayout>

【讨论】:

  • 您要检查什么?
  • “学校”单选按钮在我的 xml 中默认选中。
  • 嗯...我在 RadioButton XML 中编写代码 android:checked="true",但与上图相同。
  • 您确定在您的onCreateView() 方法中删除stateRadioGroup.clearCheck(); attendanceRadioButton.setChecked(true); 吗?
  • 是的,不是我写的!!我确定。
【解决方案2】:

尽量不要为radioButton设置检查属性。相反,将其添加到您的 radioGroup

android:checkedButton="@+id/attendanceRadioButton"

编辑:

另外,你不必初始化你的 radioButtons,你可以使用你的 radioGroup 来收听它

【讨论】:

  • 您的解决方案在我提到的这个链接中。 Link 而且,对我不起作用:(
  • 试试在你的按钮上添加android:checked="false" 并在片段中删除它们以避免任何改变它们的值。
  • 第一次试用 :: 在 RadioGroup 中添加 android:checkedButton="@+id/attendanceRadioButton" 无效!二审 :: 在 RadioGroup 中添加 android:checkedButton="@+id/attendanceRadioButton",在两个 RadioButton 中添加 android:checked="false"。不工作!
  • 如果您不介意,请查看我编辑的问题。我想我找到了这个问题发生的原因。
  • 您是否尝试将它们从视图声明中删除?也许一个 sn-p 正在弄乱它的状态。尽量不要在片段中声明 RadioButtons
猜你喜欢
  • 2012-07-15
  • 1970-01-01
  • 2011-05-22
  • 2011-05-01
  • 2018-03-09
  • 2013-09-25
  • 2021-10-29
  • 2019-12-07
  • 1970-01-01
相关资源
最近更新 更多