2012-12-04 44 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

非常感谢你 –