簡體   English   中英

如何檢查單擊按鈕時是否選擇了單選按鈕?

[英]How to check if radio buttons are selected on button click?

我正在開發一個Android應用,其中``問卷調查''活動包含作為單選按鈕和一個Button的Questions,因此當按下按鈕時,我必須檢查是否所有問題都已得到回答。消息應彈出,說明未回答特定的問題號。有人可以用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

                }


    });
}

您應該檢查radioGroupSelectedID並檢查返回ID及其對應的id ,如下所示:

0)在代碼的全局代碼中聲明所有使用的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);
            }
        }
    });

如果您提出問題,那么您的視圖就沒有任何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)並跳過第一個循環。 由你決定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM