检查是否选择了单选按钮

时间:2014-12-24 08:20:16

标签: java android xml

我有一个单选按钮组,其中包含4个单选按钮,默认情况下不会选中它们

  <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp" >

        <RadioButton
            android:id="@+id/ans11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/q"
            android:text="RadioButton"
            android:textColor="#747577"
            android:textSize="35dp" />

        <RadioButton
            android:id="@+id/ans22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ans11"
            android:text="RadioButton"
            android:textColor="#747577"
            android:textSize="35dp" />

        <RadioButton
            android:id="@+id/ans33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/memberexdone"
            android:layout_below="@+id/ans22"
            android:text="RadioButton"
            android:textColor="#747577"
            android:textSize="35dp" />

        <RadioButton
            android:id="@+id/ans44"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/memberexdone"
            android:layout_below="@+id/ans22"
            android:text="RadioButton"
            android:textColor="#747577"
            android:textSize="35dp" />
    </RadioGroup>

我试图检查是否点击按钮而没有选择任何单选按钮..

  rg = (RadioGroup) findViewById(R.id.radioGroup1);

            // get selected radio button from radioGroup
            int selectedId = rg.getCheckedRadioButtonId();

            // find the radiobutton by returned id
            rbtn = (RadioButton) findViewById(selectedId);

            String selected = rbtn.getText().toString();
            if (rg.getCheckedRadioButtonId() == -1)
            {
                //if the password less than 8 or more than 10
                AlertDialog.Builder alertBuilder3=new AlertDialog.Builder(MemberExercise.this);
                alertBuilder3.setTitle("Invalid");
                alertBuilder3.setMessage("Please select an answer first, then click on done.");
                alertBuilder3.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();


                    }
                });

                alertBuilder3.create().show();
            }
            else
            {
                .....
            }

也试过了         if(rbtn.equals(&#34;&#34;)){}

但是当我点击按钮而没有选择任何...时,应用程序将停止..为什么?

1 个答案:

答案 0 :(得分:0)

您必须在== -1之前检查getText(),否则rbtnNULLrbtn.getText()会导致NPE

    int selectedId = rg.getCheckedRadioButtonId();
    if (selectedId == -1)
    {
        //if the password less than 8 or more than 10
        AlertDialog.Builder alertBuilder3=new AlertDialog.Builder(MemberExercise.this);
        alertBuilder3.setTitle("Invalid");
        alertBuilder3.setMessage("Please select an answer first, then click on done.");
        alertBuilder3.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });

        alertBuilder3.create().show();
        return;
    }

    // find the radiobutton by returned id
    rbtn = (RadioButton) findViewById(selectedId);
    String selected = rbtn.getText().toString();