12

I would like to make a structure with the condition (if-else) RadioButton

I want that when the Radiobutton RB1 is selected, this function is active:

regAuxiliar = ultimoRegistro;

And when the radiobutton RB2 is selected, this function is active:

regAuxiliar = objRegistro;

And sorry for my English, I'm Brazilian.

Anders R. Bystrup
  • 15,324
  • 10
  • 61
  • 54
  • 3
    Please refer to the API documentation of radio button widget (http://developer.android.com/reference/android/widget/RadioButton.html) – Saurabh Jun 15 '12 at 11:48

10 Answers10

24

Just as you would with a CheckBox

RadioButton rb;

rb = (RadioButton) findViewById(R.id.rb);

rb.isChecked();
FabianCook
  • 19,591
  • 16
  • 64
  • 112
13
        if(jRadioButton1.isSelected()){
            jTextField1.setText("Welcome");
        }
        else if(jRadioButton2.isSelected()){
            jTextField1.setText("Hello");
        }
Basil Basaif
  • 352
  • 7
  • 20
6

You can also maintain a flag value based on listener,

 radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

                //handle the boolean flag here. 
                  if(arg1==true)
                         //Do something

                else 
                    //do something else

            }
        });

Or simply isChecked() can also be used to check the state of your RadioButton.

Here is a link to a sample,

http://www.mkyong.com/android/android-radio-buttons-example/

And then based on the flag you can execute your function.

Andro Selva
  • 53,136
  • 52
  • 190
  • 238
5

radiobuttonObj.isChecked() will give you boolean

if(radiobuttonObj1.isChecked()){
//do what you want 
}else if(radiobuttonObj2.isChecked()){
//do what you want 
}
Chad Bingham
  • 30,680
  • 19
  • 84
  • 114
Dheeresh Singh
  • 15,570
  • 3
  • 36
  • 36
5

If you need for espresso test the solutions is like this :

onView(withId(id)).check(matches(isChecked()));

Bye,

2

radioButton.isChecked() function returns true if the Radion button is chosen, false otherwise.

Source

st0le
  • 33,007
  • 8
  • 86
  • 89
0

You can use switch like this:

XML Layout

<RadioGroup
            android:id="@+id/RG"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/R1"
                android:layout_width="wrap_contnet"
                android:layout_height="wrap_content"
                android:text="R1" />

            <RadioButton
                android:id="@+id/R2"
                android:layout_width="wrap_contnet"
                android:layout_height="wrap_content"
                android:text="R2" />
</RadioGroup>

And JAVA Activity

switch (RG.getCheckedRadioButtonId()) {
        case R.id.R1:
            regAuxiliar = ultimoRegistro;
        case R.id.R2:
            regAuxiliar = objRegistro;
        default:
            regAuxiliar = null; // none selected
    }

You will also need to implement an onClick function with button or setOnCheckedChangeListener function to get required functionality.

Hassan Naqvi
  • 414
  • 6
  • 18
0

Simple Solution

radioSection.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(RadioGroup group1, int checkedId1) {
              switch (checkedId1) {
                  case R.id.rbSr://radiobuttonID
                      //do what you want
                      break;
                  case R.id.rbJr://radiobuttonID
                      //do what you want
                      break;
              }
          }
      });
XXIV
  • 63
  • 7
0

Check if they're checked with the el.checked attribute.

Docs or this Question

let radio1 = document.querySelector('.radio1');
let radio2 = document.querySelector('.radio2');
let output = document.querySelector('.output');

function update() {
  if (radio1.checked) {
    output.innerHTML = "radio1";
  }
  else {
    output.innerHTML = "radio2";
  }
}

update();
<div class="radios">
  <input class="radio1" type="radio" name="radios" onchange="update()" checked>
  <input class="radio2" type="radio" name="radios" onchange="update()">
</div>
<div class="output"></div>
barhatsor
  • 1,635
  • 5
  • 19
0

This worked for me:

   Espresso.onView(ViewMatchers.withId(R.id.radiobutton)).check(ViewAssertions.matches(isChecked()))
T.A
  • 11
  • 1
  • 6