not work condition onCheckedChanged

78 views Asked by At

I have an issue with the following condition:

if (Cuadrado.isChecked() == true)

This is the full code:

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

    String dato5 = DatoMath.getText().toString();

    if (Cubo.isChecked()==true) {

        if (dato5.equals("")) {
            Toast toast = Toast.makeText(getApplicationContext(), "no hay datos", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER | Gravity.LEFT, 0, 0);
            toast.show();
        } else {
            int datofinal = Integer.parseInt(dato5);
            int final3 = (int) Math.pow(datofinal, 2);

            Resultado.setText("" + final3);};

    if (Cuadrado.isChecked() == true) {

        if (dato5.equals("")) {
                    Toast toast = Toast.makeText(getApplicationContext(), "no hay datossssss", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT, 0, 0);
                    toast.show();

        } else {
                    int datofinal2 = Integer.parseInt(dato5);
                    int final4 = (int) Math.pow(datofinal2, 3);

                    Resultado.setText("" + final4);

        };
2

There are 2 answers

4
Sarthak Mittal On

Why are you intentionally complicating your code, you can simply write:

if (Cubo.isChecked())
{
//do whatever you want to do
}

The isChecked method returns a boolean so you can directly write it inside that if condition

And one more thing, why have you put a semi-colon[ ; ] after your else statements closing brace[ } ], replace that semicolon with another closing brace[ } ]

0
shiva kumar On
 @Override 
 public void onCheckedChanged(RadioGroup group, int checkedId){
switch(checkedId){
 case R.id.rbtn1:
 // radio button 1 checked related code
 break;
 case R.id.rbtn2:
 // radio button 2 checked related code
 break;
 }

}