<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rg"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.radiogroup.MainActivity" >

    <RadioButton
        android:id="@+id/rb_true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical" />

    <RadioButton
        android:id="@+id/rb_false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical" />

</RadioGroup>

i have one radio group in which i have 2 radio button

我有一个广播组,其中我有2个单选按钮

now if radio button 1 is selected and if i tap on same radio button(radio button 1) it should be unchecked ..

现在,如果选择单选按钮1,如果我点击相同的单选按钮(单选按钮1),则应该取消选中它。

it should work astoggle.

它应该工作astoggle。

3 个解决方案

#1


This should serve the purpose.

这应该有助于达到目的。

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
    @Override
    public void onCheckedChanged(RadioGroup arg0, int id)
    {
    RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(id);

    boolean isChecked = checkedRadioButton.isChecked();

    if (isChecked)
    {
    Toast.makeText(getApplicationContext(), 
               checkedRadioButton.getText(),Toast.LENGTH_LONG).show();
    radioButton = (String) checkedRadioButton.getText();
    checkedRadioButton.setChecked(false);
    }
    else
        checkedRadioButton.setChecked(true);
    }});

Also, don't forget to initialize RadioGroup

另外,不要忘记初始化RadioGroup

更多相关文章

  1. 在不同状态下使用两个图像切换按钮
  2. Android Studio 项目的导入以及依赖
  3. 如何让按钮共享android中对话框宽度的一半?
  4. 安卓,如何让两个按钮相邻,宽度相同
  5. 按下子活动后退按钮后,Android主要活动的元素不响应
  6. 抽象批处理SFTP多目的地和自动重试
  7. JSP页面中有一个按钮 点击之后执行JAVA文件
  8. [java混淆器问题] 如何让web项目的class文件不能被反编译,又能在w
  9. 不幸的是,在声明按钮时,模拟器中出现了错误

随机推荐

  1. HTML高级标签之窗口分帧(前台界面演示)
  2. easyui-tree根据叶子节点获取父节点值(N
  3. HTML中的attribute和property的区别
  4. 夜灵的Html笔记Day04——CSS3basic
  5. 网页局部打印
  6. Html5的datetime-local控件
  7. 缓存js / css资源以使我的项目脱机工作
  8. 使用html5中的base64数据播放音频。
  9. 如何在CSS中添加旋转图像?
  10. 我不能使用CSS顺序属性。