How to check if Radiobutton is verified?

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

When I select Radiobutton RB1, I want this feature to be active:

 regAuxiliar = ultimoRegistro; 

And when the RB2 radio lens is selected, this function is active:

 regAuxiliar = objRegistro; 

And sorry for my English, I'm Brazilian.

+6
source share
7 answers

Same as with CheckBox

 RadioButton rb; rb = (RadioButton) findViewById(R.id.rb); rb.isChecked(); 
+19
source
  if(jRadioButton1.isSelected()){ jTextField1.setText("Welcome"); } else if(jRadioButton2.isSelected()){ jTextField1.setText("Hello"); } 
+12
source

You can also maintain a flag value based on a 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 just isChecked() can also be used to check the status of your RadioButton.

Here is a link to the sample,

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

And then based on the flag, you can perform your function.

+6
source
Function

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

A source

+1
source

If you need an espresso test, the solutions would be:

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

Till,

+1
source

radiobuttonObj.isChecked() will give you a boolean

 if(radiobuttonObj1.isChecked()){ //do what you want }else if(radiobuttonObj2.isChecked()){ //do what you want } 
0
source

You can use the switch as follows:

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 also need to implement the onClick function with the button or setOnCheckedChangeListener function to get the required functionality.

0
source

Source: https://habr.com/ru/post/918168/


All Articles