无法检索单选按钮的状态

时间:2016-05-11 12:58:00

标签: java android radio-button

我想检索用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:

radiobutton_mode_j.isChecked()
radiobutton_mode_n.isChecked()

2 个答案:

答案 0 :(得分: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();

        }

    });

  }

答案 1 :(得分:0)

试试这个:

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
        }