2012-12-04 75 views
0

我有問題我的Android應用程序 我有兩個按鈕,如果我點擊第一個它的工作好吧,但如果我點擊第二個它做他的工作和拳頭工作 這是代碼:兩個單選按鈕不正確

用於ID

rbYes = (RadioButton) findViewById(R.id.rbYes); 
    rbNo = (RadioButton) findViewById(R.id.rbNo); 

的方法

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    // TODO Auto-generated method stub 
    switch(buttonView.getId()){ 
    case R.id.rbYes: 
     flag=true; 
     etLastHourse.setEnabled(flag); 
     etLastHourse.setBackgroundColor(Color.WHITE); 
     etLastGPA.setEnabled(flag); 
     etLastGPA.setBackgroundColor(Color.WHITE); 
     Toast.makeText(getApplicationContext(), "OK1", Toast.LENGTH_LONG).show(); 

    break; 
    case R.id.rbNo: 
     Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show(); 
     flag=false; 
     etLastHourse.setEnabled(flag); 
     etLastHourse.setEnabled(flag); 
     etLastHourse.setBackgroundColor(Color.GRAY); 
     etLastGPA.setEnabled(flag); 
     etLastGPA.setBackgroundColor(Color.GRAY); 



    break; 

    } 
} 

爲XML

<RadioButton 
      android:id="@+id/rbNo" 
      style="@style/RadioButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" 
      android:text="no" 
      /> 

     <RadioButton 
      android:id="@+id/rbYes" 
      style="@style/RadioButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" 
      android:text="yes" /> 
+0

粘貼您的佈局xml文件?你用這個單選按鈕做什麼,簡要介紹一下這個動作? – appukrb

+0

把你的radiobutton裏面radiogroups ... – appukrb

回答

1

當你第一次按下一個按鈕,撥動該按鈕只有

當你按下另一個按鈕,您切換這兩個按鈕。因此,您在第一次按下其中一個單選按鈕時會檢查您的onCheckedChanged,並在下次按兩次。

http://developer.android.com/guide/topics/ui/controls/radiobutton.html

public void onRadioButtonClicked(View view) { 
    // Is the button now checked? 
    boolean checked = ((RadioButton) view).isChecked(); 

    // Check which radio button was clicked 
    switch(view.getId()) { 
     case R.id.radio_pirates: 
     if (checked) 
      // Do all things here for this button 
     break; 
     case R.id.radio_ninjas: 
     if (checked) 
      // Do all things here for this button 
     break; 
} 

兩者只是做你想做如果按鈕被選中的事物。

+0

那麼有什麼解決方案? –

+0

檢查編輯... – Zyber

+0

非常感謝你 –