【问题标题】:How to check if radio buttons are selected on button click?如何检查按钮单击时是否选择了单选按钮?
【发布时间】:2014-04-09 07:14:27
【问题描述】:

我正在开发一个 android 应用程序,其中的 Questionnaire 活动包含作为单选按钮和按钮的问题。因此,当按下按钮时,我必须检查是否所有问题都已回答。如果未回答问题然后应该会弹出一条警报消息,说明没有回答特定的问题。任何人都可以帮助我处理 Java 代码。 提前致谢。

<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

                }


    });
}

【问题讨论】:

  • 只是您将来可能面临的问题。如果您打算使用RadioGroup 及其listener,请记住它会多次引发事件(准确地说是3 次)。 Here 是一些确认。

标签: java android onclick radio-button onclicklistener


【解决方案1】:

您应该检查radioGroup 的SelectedID 并检查返回ID 及其对应的ids,如下所示:

0)在你的代码的全局scoop中声明所有使用的UI组件如下:

Class x extends Activity{
    RadioButton radioButton1;
    ...

1)初始化所有XML按钮以获取OnCreate中的每个按钮ID如下

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

2)在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
}
}
});

用你的休息检查代替点。

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

Button next;
    RadioGroup rg1;
    RadioGroup rg2;
    RadioGroup rg3;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_questionnaire1);
    rg1=(RadioGroup)findViewById(R.id.Mquestion1);
    rg2=(RadioGroup)findViewById(R.id.Mquestion2);
    rg3=(RadioGroup)findViewById(R.id.Mquestion3);
    next=(Button)findViewById(R.id.button1);
    next.setOnClickListener(new View.OnClickListener()
    {
        public void OnClick(View v)
        {
            if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || Mquestion1.getCheckedRadioButtonId()!=R.id.radioButton2 || Mquestion1.getCheckedRadioButtonId()!=R.id.radioButton3)||(Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton4 || Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton5 || Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton6)||(Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton7 || Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton8 || Mquestion3.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);
            }
        }
    });

【讨论】:

  • @Steve 检查修改后的答案
  • @Steve 检查修改后的答案
  • 将鼠标光标放在错误行的开头,点击(CTRL+1),选择import(如果存在),选择add unimplemented method
  • 如果您发现我的信息有用,请投票给我的答案或接受它作为答案
  • 写next.setOnClickListener 并按回车键。然后将光标放在解析中并写new 然后(CTRL +空格)并选择onClick...
【解决方案2】:

如果您生成问题,因此您的视图没有任何 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),跳过第一个循环。这取决于你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    相关资源
    最近更新 更多