【问题标题】:Can't retrieve state of radio button无法检索单选按钮的状态
【发布时间】:2016-09-06 21:37:55
【问题描述】:

我要检索用 XML 创建的单选按钮。没关系。但是当我检查这个单选按钮是否被选中时,Android Studio 告诉我单选按钮检索是空的。

XML:

<RadioGroup
    android:layout_width="148dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:id="@+id/RadioGroup_MODE">
    <!-- android:buttonTint="@color/BouttonsRadio"-->

    <RadioButton
        android:layout_width="137dp"
        android:layout_height="wrap_content"
        android:text="@string/Mode_J"
        android:id="@+id/BouttonRADIO_JOUR"
        android:layout_gravity="center_horizontal"
        android:textSize="20dp"
        android:textStyle="bold" />

    <RadioButton
        android:layout_width="137dp"
        android:layout_height="wrap_content"
        android:text="@string/Mode_N"
        android:id="@+id/BouttonRADIO_NUIT"
        android:layout_gravity="center_horizontal"
        android:textSize="20dp"
        android:textStyle="bold" />

</RadioGroup>

JAVA:

final RadioButton radiobutton_mode_j = (RadioButton) findViewById(R.id.BouttonRADIO_JOUR);
    final RadioButton radiobutton_mode_n = (RadioButton) findViewById(R.id.BouttonRADIO_NUIT);

我使用这段代码来检查是否是这个单选按钮被选中:

radiobutton_mode_j.isChecked()
radiobutton_mode_n.isChecked()

【问题讨论】:

    标签: java android radio-button


    【解决方案1】:

    试试下面的代码:

         int selectedId = radioGroup.getCheckedRadioButtonId();
    
         RadioButton radioButton = (RadioButton) findViewById(selectedId);
    
         Toast.makeText(MyAndroidAppActivity.this,
                        radioButton.getText(), Toast.LENGTH_SHORT).show();
    

    编辑

    这是根据您的要求提供的完整代码:

    xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
       <RadioGroup
        android:layout_width="148dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:id="@+id/RadioGroup_MODE">
        <!-- android:buttonTint="@color/BouttonsRadio"-->
    
        <RadioButton
            android:layout_width="137dp"
            android:layout_height="wrap_content"
            android:text="@string/Mode_J"
            android:id="@+id/BouttonRADIO_JOUR"
            android:layout_gravity="center_horizontal"
            android:textSize="20dp"
            android:textStyle="bold" />
    
        <RadioButton
            android:layout_width="137dp"
            android:layout_height="wrap_content"
            android:text="@string/Mode_N"
            android:id="@+id/BouttonRADIO_NUIT"
            android:layout_gravity="center_horizontal"
            android:textSize="20dp"
            android:textStyle="bold" />
    
    </RadioGroup>
    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />
    

    活动:

     private RadioGroup radioGroup;
      private RadioButton radioButton;
      private Button btnDisplay;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
    
        radioGroup = (RadioGroup) findViewById(R.id.RadioGroup_MODE);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);
    
        btnDisplay.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                    // get selected radio button from radioGroup
                int selectedId = radioGroup.getCheckedRadioButtonId();
    
                // find the radiobutton by returned id
                    radioButton = (RadioButton) findViewById(selectedId);
    
                Toast.makeText(MyAndroidAppActivity.this,
                    radioButton.getText(), Toast.LENGTH_SHORT).show();
    
            }
    
        });
    
      }
    

    【讨论】:

    • 感谢您的回答。但我是 Android 开发的初学者,我不明白你的变量“radioButton”是什么
    • @McNavy 好的,我正在更新我的答案。
    • @McNavy 你不需要像你一样初始化radioButtons,只要按照上面的代码,如果有用就接受。
    • 谢谢,但我有一个带有两个单选按钮的收音机组。我不知道如何使用我的代码调整您的解决方案。请问可以适应我吗?
    • @McNavy 你试过上面的代码吗?请告诉。如果您觉得答案有帮助并解决了您的问题,请接受答案并点赞。在此先感谢:)
    【解决方案2】:

    试试这个:

    RadioGroup rg = (RadioGroup) v.findViewById(R.id.RadioGroup_MODE);
    if(rg.getCheckedRadioButtonId() != -1){
                //one of the Radiobuttons is checked
    
            }else {
                //none of the Radiobuttons is checked
            }
    

    【讨论】:

    • 谢谢,但我有一个带有两个单选按钮的收音机组。我不知道如何使用我的代码调整您的解决方案。请问可以适应我吗?
    猜你喜欢
    • 2018-07-23
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 2021-05-20
    • 2012-06-29
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多