2014-02-27 113 views
0

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

主要计算工作正常,但在检查了很多教程后,我无法使我找到的代码适应我的应用程序。

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> 
+3

非常好!但现在的问题是什么? – user2450263

+0

我不知道如何让选中的单选按钮将他的ID与以下条件相关联!上面的代码是错误的和不完整的! –

回答

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和准单选按钮

+0

似乎很简单! 我是否需要创建一个名为radioSexButton的变量 –

+0

我应该如何在if条件中表达该变量? if(radioSexButton =“radioMasc”){ } –

+0

检查radioSexButton.getText() – user2450263