Omar Al-hamawi
Omar Al-hamawi

Reputation: 37

two radio buttons not do it correctly

I have problem my android app I have two buttons if i click at the first its work ok,but if i click at the second its do his job and fist job this is the code:

for the id

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

for the method

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;

    }
}

for the 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" />

Upvotes: 0

Views: 265

Answers (1)

Zyber
Zyber

Reputation: 1032

When you press one button first, you toggle that button only

When you press the other button, you toggle both buttons. Therefore your onCheckedChanged is checked once the first time you press one of the radiobuttons, and two times the next time.

Taken from 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;
}

Just do the things you want to if the button is checked.

Upvotes: 1

Related Questions