2014-03-06 201 views
2

我正在開發一個android應用程序,其中的調查問卷活動包含作爲單選按鈕和Button.So當按鈕被按下時,我要檢查是否所有的問題都回答。如果問題沒有得到解答,然後應該彈出一個警告消息,說明具體的問題沒有回答。可以任何人請幫助我的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 

       } 


    }); 
} 
+0

只是你將來可能面對的事情。如果你打算使用'RadioGroup'和它的['listener'](http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html),請記住它會引發事件倍數(3 ,確切地說)次。 [Here](http://stackoverflow.com/questions/10263778/radiogroup-calls-oncheckchanged-three-times)是一些確認。 –

回答

1

您應該檢查在radioGroupSelectedID並檢查返回ID及其相應的id S作爲如下:

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); 
      } 
     } 
    }); 
+0

@Steve檢查修改後的答案 –

+1

@Steve檢查修改後的答案 –

+0

將鼠標光標置於錯誤行的開頭並單擊(CTRL + 1)並選擇導入(如果存在)並選擇添加未實現的方法 –

0

如果您生成您的問題,所以你不要有任何的ID爲您的看法,您可以通過所有RadioGroups並通過其在RadioGroup中所有兒童週期。所以,如果你存儲例如在ListArray radioGroups 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)並跳過第一個循環。隨你便。

相關問題