【发布时间】: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 有关系。但我不知道如何解决这个问题。所以,请帮助我:(
在这种情况下,发生了上情况。
有MainActivity 和RadioButtonFragment,我将从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