You are on page 1of 2

package com.example.

radiobutton;

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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private EditText et1, et2;
private TextView tv1;
private RadioButton rb_su, rb_re, rb_mu, rb_di;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et1 = (EditText) findViewById(R.id.txt_num1);


et2 = (EditText) findViewById(R.id.txt_num2);
rb_su = (RadioButton) findViewById(R.id.rb_suma);
rb_re = (RadioButton) findViewById(R.id.rb_resta);
rb_mu = (RadioButton) findViewById(R.id.rb_producto);
rb_di = (RadioButton) findViewById(R.id.rb_division);
tv1 = (TextView) findViewById(R.id.tv_1);

public void calculadora(View view) {


String valor_string1 = et1.getText().toString();
String valor_string2 = et2.getText().toString();

int valor_int1 = Integer.parseInt(valor_string1);


int valor_int2 = Integer.parseInt(valor_string2);

if (rb_su.isChecked() == true) {
int suma = valor_int1 + valor_int2;
String resultado = String.valueOf(suma);
tv1.setText(resultado);
} else if (rb_re.isChecked() == true) {
int resta = valor_int1 - valor_int2;
String resultado = String.valueOf(resta);
tv1.setText(resultado);
} else if (rb_mu.isChecked()== true) {
int multiplicacion = valor_int1 * valor_int2;
String resultado = String.valueOf(multiplicacion);
tv1.setText(resultado);
}else if (rb_di.isChecked()==true){

if (valor_int2 != 0){
int division = valor_int1 / valor_int2;
String resultado = String.valueOf(division);
tv1.setText(resultado);

}else {
Toast.makeText(this, "el segundo valor debe de ser diferente de
cero", Toast.LENGTH_LONG).show();
}
}
}
}

You might also like