Java RadioButton BMI计算器(Android)

时间:2014-02-27 17:39:16

标签: java android xml

我正在将我的frist Android应用程序构建到学校项目中。它基于BMI计算器,最后的结果将受到性别(男/女)的影响。

主要计算是有效的,但在检查了很多教程后,我无法调整我在我的应用程序中找到的代码。

package com.example.calculadorimc;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {


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

    public void calculateClickHandler(View view) {
        // make sure we handle the click of the calculator button

        if (view.getId() == R.id.botaoCalcular) {

         // get the references to the widgets
         EditText editPeso = (EditText)findViewById(R.id.editPeso);
         EditText editAltura = (EditText)findViewById(R.id.editAltura);
         TextView imcView = (TextView)findViewById(R.id.imcView);

         // get the users values from the widget references

         float peso = Float.parseFloat(editPeso.getText().toString());
         float altura = Float.parseFloat(editAltura.getText().toString());

         // calculate the bmi value

         float imcValue = calcularIMC(peso, altura);

         // interpret the meaning of the bmi value
         String imcInterpretation = interpretIMC(imcValue);

         // now set the value in the result text

         imcView.setText(imcValue + "-" + imcInterpretation);
        }
       }

       // the formula to calculate the BMI index

       private float calcularIMC (float peso, float altura) {

        return (float) (peso / (altura * altura));
       }


       // interpret what BMI means
       private String interpretIMC(double imcValue) {

            // Check which radio button was clicked
            if(RadioButton.R.id.radioMasc){
                         if (imcValue < 16) {
                             return "Magreza Grave";
                            } else if (imcValue < 18.5) {

                             return "Magreza Leve";
                            } else if (imcValue < 25) {

                             return "Saudável";
                            } else if (imcValue < 30) {

                             return "Sobrepeso";
                            } else if (imcValue < 35){
                             return "Obesidade Grau I";
                            } else if (imcValue < 40){
                             return "Obesidade Grau II";
                            } else {
                                return "Obesidade Grau III";
                            }}
                    break;
            if(RadioButton.R.id.radioFem){
                    if (checked){
                        if (imcValue < 16) {
                         return "Magreza Grave";
                        } else if (imcValue < 18.5) {

                         return "Magreza Leve";
                        } else if (imcValue < 25) {

                         return "Saudável";
                        } else if (imcValue < 30) {

                         return "Sobrepeso";
                        } else if (imcValue < 35){
                         return "Obesidade Grau I";
                        } else if (imcValue < 40){
                         return "Obesidade Grau II";
                        } else {
                            return "Obesidade Grau III";
                        }}
                    }
                    break;
       }    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我决定用旧的实验来清理应用程序,只让代码的逻辑显示受性别影响的结果。

这是我的.XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <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="@string/radioMasc" />

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

    <TextView
        android:id="@+id/alturaView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/alturaView" />

    <EditText
        android:id="@+id/editAltura"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="5"
        android:inputType="numberDecimal"
        android:text="@string/editAltura" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/pesoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pesoView" />

    <EditText
        android:id="@+id/editPeso"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="5"
        android:inputType="number"
        android:text="@string/editPeso" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:layout_marginBottom="5dip"
        android:layout_marginTop="5dip"
        android:background="#111111" />

    <Button
        android:id="@+id/botaoCalcular"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="calculateClickHandler"
        android:text="@string/botaoCalcular" />

    <TextView
        android:id="@+id/imcView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/imcView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

 // get selected radio button from radioGroup
    int selectedId = radioSexGroup.getCheckedRadioButtonId();

 // find the radiobutton by returned id
    radioSexButton = (RadioButton) findViewById(selectedId);

//  radioSexButton.getText()  if you want the text

即。获取id和关联单选按钮