lundi 24 mai 2021

BMI Button checked for gender

I have a code where I'm stuck on something I don't know how to do. I have to develop an app in java of ideal weight, where male calculates (72.7height) - 58 and female calculates (62.1height) - 44.7. I created a radio button for sex, but I can't know where I can put the if code below that I found. I already appreciate the help

activity_main.xml

 <RadioGroup
            android:id="@+id/rgSexo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rbMasculino"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Masculino" />

            <RadioButton
                android:id="@+id/rbFeminino"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Feminino" />
        </RadioGroup>

mainActivity.java


public class MainActivity extends AppCompatActivity {

        double altura;
        double resp;

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

        final EditText etAltura = findViewById(R.id.etAltura);
        Button btnCalc = findViewById(R.id.btnCalc);
        final TextView tvResp = findViewById(R.id.tvResp);



        // Botão para calcular
        btnCalc.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                try{
                    double imc = calcIMC( Double.parseDouble(etAltura.getText().toString()));
                    imcString(imc, tvResp);
                }
                catch (Exception e) {
                    Log.d("Erro:",e.toString());
                    tvResp.setText(getText(R.string.err));
                }
                hideKeyBoard();
            }
        });

    }



    // Cálculo do IMC
    private double calcIMC( double altura){
        


        double resp = (72.7*altura)-58;
        return resp;
    }



    // Obtem cálculo e formula resposta
    private void imcString(double calc,TextView tv){
        DecimalFormat df = new DecimalFormat("#.00");
        String resp = "Seu peso ideal é: "+df.format(calc)+"\n";

        tv.setText(resp);
    }

The question is, where exactly I'll put this

public void onRadioButtonClicked(View v)
    {
        //require to import the RadioButton class
        RadioButton rb1 = (RadioButton) findViewById(R.id.Radio_one);
        RadioButton rb2 = (RadioButton) findViewById(R.id.Radio_two);

        //is the current radio button now checked?
        boolean  checked = ((RadioButton) v).isChecked();

        //now check which radio button is selected
        //android switch statement
        switch(v.getId()){

            case R.id.Radio_one:
                if(checked)
                    //if first selected
                break;

            case R.id.Radio_two:
                if(checked)
                    //if second selected

                break; 
        }

Aucun commentaire:

Enregistrer un commentaire