如何检查单击按钮时是否选择了单选按钮?

时间:2014-03-06 14:57:42

标签: java android onclick radio-button onclicklistener

我正在开发一个Android应用程序,其中问卷调查活动包含问题,作为单选按钮和按钮。按下按钮时,我要检查是否所有问题都已回答。如果问题没有得到解答然后会弹出一条警告消息,说明特定的问题没有得到解答。任何人都可以帮我解决java代码问题。 在此先感谢。

This is my Questionnaire with radio buttons and a button placed below

<RadioGroup
    android:id="@+id/Mquestion1"  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb1" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb2" />

<RadioButton
    android:id="@+id/radioButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb3" />
</RadioGroup>
<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_view"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="@+id/Mquestion2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb1" />

<RadioButton
    android:id="@+id/radioButton5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb2" />

<RadioButton
    android:id="@+id/radioButton6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb3" />
</RadioGroup>
<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_view"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="@+id/Mquestion3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb1" />

<RadioButton
    android:id="@+id/radioButton8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb2" />

<RadioButton
    android:id="@+id/radioButton9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb3" />
</RadioGroup>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="44dp" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="sendMessage"
        android:text="@string/MQ1_next" />

</RelativeLayout>

</LinearLayout>

这是java代码

public class ManagerQuestionnaire1 extends Activity
{

Button next;
RadioGroup rg1;
RadioGroup rg2;
RadioGroup rg3;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_questionnaire1);
    final RadioGroup    rg1=(RadioGroup)findViewById(R.id.Mquestion1);
     final RadioGroup   rg2=(RadioGroup)findViewById(R.id.Mquestion2);
     final RadioGroup   rg3=(RadioGroup)findViewById(R.id.Mquestion3);
    Button next=(Button)findViewById(R.id.button1);
            next.setOnClickListener(new View.OnClickListener()
            {
                public void OnClick(View v) //Here in this line I'm getting a caution symbol which says REMOVE METHOD 'OnClick'
                {

                    if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || rg1.getCheckedRadioButtonId()!=R.id.radioButton2 || rg1.getCheckedRadioButtonId()!=R.id.radioButton3)||(rg2.getCheckedRadioButtonId()!=R.id.radioButton4 || rg2.getCheckedRadioButtonId()!=R.id.radioButton5 || rg2.getCheckedRadioButtonId()!=R.id.radioButton6)||(rg3.getCheckedRadioButtonId()!=R.id.radioButton7 || rg3.getCheckedRadioButtonId()!=R.id.radioButton8 || rg3.getCheckedRadioButtonId()!=R.id.radioButton9))
                    {
                         AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
                         alert.setTitle("Exception:Complete the Questions");
                         alert.setMessage("Please ensure all Questions are answered");
                    }
                    else    
                    {
                         Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
                            startActivity(intent);
                    }
                }

                @Override
                public void onClick(View v) 
                {
                    // TODO Auto-generated method stub

                }


    });
}

2 个答案:

答案 0 :(得分:1)

您应该查看SelectedID的{​​{1}}并检查返回radioGroup及其对应的ID,如下所示:

0)在代码的全局变量中声明所有使用过的UI组件,如下所示:

id

1)初始化所有Class x extends Activity{ RadioButton radioButton1; ... 按钮以获取XML中的每个按钮ID,如下所示

OnCreate

2)在radioButton1 = (RadioButton) findViewById(R.id.radioButton1); //The rest of other buttons .... ....

clickListener

用休息检查替换点。

使用以下代码代替您的代码

next.setOnClickListener(new View.OnClickListener()
{
 public void OnClick(View v)
{
 if(Mquestion1.value!=radioButton1 || Mquestion1.value!=radioButton2 || Mquestion1.value!=radioButton3 || Mquestion1.value!=radioButton4) ||(Mquestion2.valu!= radioButton5 || ..... )||(Mquestion3.value....)
{
      //Show the allert
}
}
});

答案 1 :(得分:0)

如果您生成了问题,那么您的视图没有任何ID,您可以遍历所有RadioGroup以及RadioGroup中的所有子项。因此,如果您将RadioGroups存储在ListArray radioGroups中,那么您可以这样做:

private boolean isAllAnswered() {
    for (RadioGroup rg : radioGroups) {
        boolean questionAnswered = false;
        for (int i = 0; i < rg.getChildCount(); i++) { 
            RadioButton rb = rg1.getChildAt(i);
            if (rb.isChecked()) {
                questionAnswered = true;
                break; // litle optimalization
            }
        }
        if (questionAnswered == false) {
            return false;
        }
    }
    return true;
}

如果您需要知道哪些问题没有得到解答,可以将其扩展为返回数组。

如果你使用xml,那么做一个常见的方法isAnswered(RadioGroup rg)并跳过第一个循环。这取决于你。