Android Studio radio button

agregamos  el radioGroup para poder ajuntar todos los radiobutton

y para verificar que se ha seleccionado algun radio button solo tenemos que llamar al metodo del radioButton ischecked() y listo

algo asi quedario en el xml de android studio

<RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/rbSumar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Sumar" />
<RadioButton android:id="@+id/rbRestar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Restar" /> </RadioGroup>


y en el codigo de obtencion de los atributos. Solo bastaria con verificar si estan seleccionados o no
if(rbRestar.isChecked()){
Toast.makeText(this,"restar",Toast.LENGTH_LONG).show(); respuesta=numero1-numero2;
}else{
respuesta=numero1+numero2;
}



Comentarios