You are on page 1of 4

GUIA CAPTURAR RADIO BOTON

 Archivo.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Ingrese el primer valor"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Ingrese el 2do valor"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et1" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="187dp"
android:layout_height="274dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et2">

<RadioButton
android:id="@+id/r1"
android:layout_width="117dp"
android:layout_height="36dp"
android:layout_marginStart="16dp"
android:layout_marginTop="44dp"
android:text="sumar" />

<RadioButton
android:id="@+id/r2"
android:layout_width="112dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="restar" />

<RadioButton
android:id="@+id/r3"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:text="potencia" />

<RadioButton
android:id="@+id/r4"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:text="combinatorio" />
</RadioGroup>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="136dp"
android:layout_marginTop="64dp"
android:onClick="operar"
android:text="Operar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="64dp"
android:text="Resultado"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>

 Archivo.java
 package com.example.radiogroupprpuestos;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

private EditText et1,et2;


private TextView tv1;
private RadioButton r1,r2,r3,r4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et1=findViewById(R.id.et1);
et2=findViewById(R.id.et2);
tv1=findViewById(R.id.tv1);
r1=findViewById(R.id.r1);
r2=findViewById(R.id.r2);
r3=findViewById(R.id.r3);
r4=findViewById(R.id.r4);

public void operar(View view) {


String valor1=et1.getText().toString();
String valor2=et2.getText().toString();
int nro1=Integer.parseInt(valor1);
int nro2=Integer.parseInt(valor2);
if (r1.isChecked()==true) {
int suma=nro1+nro2;
String resu=String.valueOf(suma);
tv1.setText(resu);
} else
if (r2.isChecked()==true) {
int resta=nro1-nro2;
String resu=String.valueOf(resta);
tv1.setText(resu);

}else
if (r3.isChecked()==true) {
double resul= Math.pow(nro1,nro2);
String resu=String.valueOf(resul);
tv1.setText(resu);
}else
if (r4.isChecked()==true) {
int fm=1;
int fn=1;
int fmn=1;
for(int k=1;k<=nro1;++k){
fm=fm*k;
}
for(int k=1;k<=nro1;++k){
fn=fn*k;
}
for(int k=1;k<=nro1-nro2;++k){
fmn=fmn*k;
}
int resul=fm/(fn*fmn);
String resu=String.valueOf(resul);
tv1.setText(resu);

}
}
}

You might also like