我的第一个Android程序,BMI计算器,分享一下源码

本人电子专业学生,最近要交嵌入式报告,自学了Android相关的知识,整出来了一个Android版的 BMI计算器,

在安卓模拟器中运行情况如下,自我感觉还行,分享一下代码

MainActivity.java

  1 package com.example.bmicalculator1;

  2 

  3 //默认import的两个package

  4 import android.app.Activity;

  5 import android.os.Bundle;

  6 

  7 import java.text.DecimalFormat;

  8 //import java.lang.Math.lang;

  9 import com.example.bmicalculator1.R.string;

 10 

 11 import android.view.View;

 12 import android.view.View.OnClickListener;

 13 import android.widget.Button;

 14 import android.widget.EditText;

 15 import android.widget.RadioButton;

 16 import android.widget.RadioGroup;

 17 import android.widget.CheckBox;

 18 import android.widget.TextView;

 19 import android.app.AlertDialog;//弹窗

 20 import android.content.res.ColorStateList;

 21 import android.graphics.Color;

 22 import android.widget.Toast;   //提示

 23 

 24 //声明一个 public class,名字叫MainActivity

 25 //extends是继承(inherit)的关键字

 26 //Activity是所导入的包

 27 public class MainActivity extends Activity

 28 {

 29     //定义各种ko变量

 30     private EditText et_height=null;

 31     private EditText et_weight=null;

 32     private Button   bt_submit=null;

 33     private Button   bt_exit=null;

 34     private TextView tv_result=null;

 35     private TextView tv_suggest=null;

 36     private TextView tv_target_range=null;

 37     private RadioGroup  rg1_height=null;

 38     private RadioButton rg1_cms = null;

 39     private RadioButton rg1_inchs = null;

 40     private RadioGroup  rg2_weight=null;  

 41     private RadioButton rg2_kgs = null;

 42     private RadioButton rg2_lbs = null;      

 43     private double height=0;

 44     private double weight=0;

 45     private double heightscale=1.0;

 46     private double weightscale=1.0;

 47     

 48     /** Called when the activity is first created**/

 49     @Override  //重写onCreate()这个方法,表示创建时进行一些处理

 50     public void onCreate(Bundle savedInstanceState)

 51     {

 52         super.onCreate(savedInstanceState);

 53         setContentView(R.layout.activity_main);//设置显示界面

 54         findViewID();  //获取响应的控件

 55         setListensersON(); //设置监听器

 56 

 57     }

 58        

 59     private void findViewID(){    

 60         et_height=(EditText)findViewById(R.id.et_height);

 61         et_weight=(EditText)findViewById(R.id.et_weight);

 62         bt_submit=(Button)findViewById(R.id.bt_submit);

 63         bt_exit=(Button)findViewById(R.id.bt_exit);

 64         tv_result=(TextView)findViewById(R.id.tv_result); 

 65         tv_target_range=(TextView)findViewById(R.id.tv_target_range);

 66         tv_suggest=(TextView)findViewById(R.id.tv_suggest);  

 67         rg1_height=(RadioGroup)findViewById(R.id.rg1_height);

 68         rg2_weight=(RadioGroup)findViewById(R.id.rg2_weight);

 69     }

 70             

 71     //使用setOnClickListener开启监听事件

 72     private void setListensersON(){        

 73       bt_submit.setOnClickListener(bt_listener);

 74       bt_exit.setOnClickListener(bt_listener);

 75       rg1_height.setOnCheckedChangeListener(rg1_listener);

 76       rg2_weight.setOnCheckedChangeListener(rg2_listener);

 77     }    

 78     

 79     

 80     //监听单选按钮组rg1_listener()

 81     private RadioGroup.OnCheckedChangeListener rg1_listener=

 82             new RadioGroup.OnCheckedChangeListener(){

 83         @Override

 84         public void onCheckedChanged(RadioGroup group, int checkedId){

 85             if(checkedId == R.id.rg1_cms)

 86             {

 87                 heightscale=1.0;

 88                 //rg1_cms.setTextColor(Color.rgb(200,100,0));

 89                 Toast.makeText(MainActivity.this,getText(string.str_rg1_cms),Toast.LENGTH_LONG).show();

 90             }

 91             else if(checkedId == R.id.rg1_inchs)

 92             {

 93                 heightscale=2.54;

 94                 Toast.makeText(MainActivity.this,getText(string.str_rg1_inchs),Toast.LENGTH_LONG).show();

 95             }

 96         }

 97         

 98     };

 99 

100     private RadioGroup.OnCheckedChangeListener rg2_listener=

101             new RadioGroup.OnCheckedChangeListener(){

102         @Override

103         public void onCheckedChanged(RadioGroup group, int checkedId){

104             if(checkedId == R.id.rg2_kgs)

105             { 

106                 weightscale=1.0;

107                 Toast.makeText(MainActivity.this,getText(string.str_rg2_kgs),Toast.LENGTH_LONG).show();

108             }

109             else if(checkedId == R.id.rg2_lbs)

110             {

111                 weightscale=0.45359;

112                 Toast.makeText(MainActivity.this,getText(string.str_rg2_lbs),Toast.LENGTH_LONG).show();

113             }

114         }

115         

116     };

117     

118     // Button.OnClickListener来自于android.widget.Button

119     private Button.OnClickListener bt_listener = new Button.OnClickListener(){

120       public void onClick(View v)

121       {

122           Button button=(Button)v;

123           switch(button.getId())

124           {

125               case R.id.bt_submit:

126               {

127                   DecimalFormat nf=new DecimalFormat("0.00");                 

128                   try   { weight=weightscale*Double.parseDouble(et_weight.getText().toString());  } 

129                   catch (Exception e)    { weight=63*weightscale;  }

130                     try   { height=heightscale*Double.parseDouble(et_height.getText().toString())/100; } 

131                     catch (Exception e)    { height=heightscale*1.73;}

132                     

133                   System.out.println(height);  //输出身高

134                   System.out.println(weight);  //输出体重

135                   

136                   //BMI计算公式

137                   //Current formula: BMI = weight(kg)/height(m)^2 = 703*weight(lb)/height(in)^2.

138                   //New formula: BMI = 1.3*weight(kg)/height(m)^2.5 = 5734*weight(lb)/height(in)^2.5

139                   double oldBMI = weight/(height*height);

140                   double newBMI = 1.3*weight/(height*height*Math.sqrt(height));

141 

142                   //计算健康体重的范围

143                   double targetupper = 25*(height*height*Math.sqrt(height))/1.3/weightscale;

144                   double targetlower = 18.5*(height*height*Math.sqrt(height))/1.3/weightscale;

145                   //根据选项,获取体重的单位

146                     String units_weight=(String) (weightscale==1.0?getText(string.str_rg2_kgs):getText(string.str_rg2_lbs));

147                   if(oldBMI>0 )

148                   {

149                       tv_result.setText(getText(R.string.str_old_result)+nf.format(oldBMI)+","+getText(R.string.str_new_result)+nf.format(newBMI));

150                       tv_target_range.setText("健康体重范围:"+nf.format(targetlower)+"~"+nf.format(targetupper)+units_weight);               

151                       if(oldBMI <=18.5 ) 

152                       {

153                         tv_suggest.setText(R.string.advice_light);

154                       }

155                       else if(oldBMI <= 24)

156                       {

157                         tv_suggest.setText(R.string.advice_average);

158                         //tv_suggest.setTextColor();

159                           Toast.makeText(MainActivity.this, getText(string.str_Great),Toast.LENGTH_LONG).show();            

160                       }

161                       else if(oldBMI <= 28)

162                       {

163                         tv_suggest.setText(R.string.advice_overweight);

164                       }

165                       else 

166                       {

167                         tv_suggest.setText(R.string.advice_fat);

168                       }

169                       break;                   

170                                                      

171                   }               

172                   else

173                   {

174                       Toast.makeText(MainActivity.this,getText(string.str_errorin),Toast.LENGTH_LONG).show();

175                       break;

176                   } 

177                   

178                   

179               }

180               case R.id.bt_exit:

181               {

182                   System.exit(0);//结束activity并释放资源

183                   break;

184               }

185           

186           }

187           

188       }

189 

190     };

191         

192   }

193        
View Code

 

activity_main.xml

  1 <?xml version="1.0" encoding="utf-8"?>

  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3     android:layout_width="match_parent"

  4     android:layout_height="match_parent"

  5     android:background="#E6E6FA"

  6     android:orientation="vertical" >

  7 

  8     <LinearLayout

  9         android:layout_width="match_parent"

 10         android:layout_height="30dp"

 11         android:background="#807CFC00"

 12         android:orientation="vertical" >

 13 

 14         <TextView

 15             android:id="@+id/title"

 16             android:layout_width="110dp"

 17             android:layout_height="match_parent"

 18             android:layout_gravity="center_horizontal"

 19             android:ellipsize="marquee"

 20             android:focusable="true"

 21             android:focusableInTouchMode="true"

 22             android:marqueeRepeatLimit="marquee_forever"

 23             android:singleLine="true"

 24             android:text="@string/title"

 25             android:textSize="16sp" />

 26 

 27     </LinearLayout>

 28 

 29   <LinearLayout

 30     android:layout_width="match_parent"

 31     android:layout_height="match_parent"

 32     android:background="#E6E6FA"

 33     android:paddingLeft="10dp"

 34     android:paddingRight="10dp"

 35     android:orientation="vertical" >

 36     <TextView  

 37         android:layout_width="wrap_content" 

 38         android:layout_height="wrap_content" 

 39         android:text="@string/height"

 40         android:padding="2dp"        

 41         android:textStyle="bold"

 42         android:textSize="20sp"

 43         android:layout_marginTop="20dp" 

 44         />

 45     

 46     <LinearLayout

 47         android:orientation="horizontal"

 48         android:layout_width="fill_parent"

 49         android:layout_height="wrap_content">            

 50         <EditText

 51             android:id="@+id/et_height"

 52             android:layout_width="140dp"

 53             android:layout_height="wrap_content"

 54             android:inputType="numberDecimal"

 55             android:paddingLeft="10dp"

 56             android:paddingRight="10dp"

 57             android:hint="@string/myHeight"

 58         />

 59         <RadioGroup

 60             android:id="@+id/rg1_height"

 61             android:layout_width="match_parent"

 62             android:layout_height="wrap_content"

 63             android:orientation="horizontal">

 64             <RadioButton

 65                 android:id="@+id/rg1_cms"

 66                 android:text="@string/str_rg1_cms"

 67                 android:checked="true"

 68                 android:layout_width="80dip">              

 69             </RadioButton>

 70             <RadioButton

 71                 android:id="@+id/rg1_inchs" 

 72                 android:text="@string/str_rg1_inchs"

 73                 android:layout_width="80dip">              

 74             </RadioButton>

 75         </RadioGroup>

 76     </LinearLayout>

 77     

 78     <TextView  

 79         android:layout_width="wrap_content" 

 80         android:layout_height="wrap_content" 

 81         android:text="@string/weight"

 82         android:padding="2dp"

 83         android:textStyle="bold"

 84         android:textSize="20sp"

 85         android:paddingLeft="20dp"

 86         android:paddingRight="10dp"

 87         />

 88     

 89     <LinearLayout

 90         android:orientation="horizontal"

 91         android:layout_width="fill_parent"

 92         android:layout_height="wrap_content">

 93         <EditText

 94             android:id="@+id/et_weight"

 95             android:layout_width="140dp"

 96             android:layout_height="wrap_content"

 97             android:inputType="numberDecimal"

 98             android:paddingLeft="10dp"

 99             android:paddingRight="10dp"

100             android:hint="@string/myWeight"    

101         />

102         <RadioGroup

103             android:id="@+id/rg2_weight"

104             android:layout_width="fill_parent"

105             android:layout_height="wrap_content"

106             android:orientation="horizontal">

107             <RadioButton

108                 android:id="@+id/rg2_kgs"

109                 android:text="@string/str_rg2_kgs"

110                 android:checked="true"

111                 android:layout_width="80dip">              

112             </RadioButton>

113             <RadioButton

114                 android:id="@+id/rg2_lbs" 

115                 android:text="@string/str_rg2_lbs"

116                 android:layout_width="80dip"> 

117             </RadioButton>

118         </RadioGroup>

119     </LinearLayout>

120     

121     <LinearLayout

122         android:orientation="horizontal"

123         android:layout_width="fill_parent"

124         android:layout_height="wrap_content"

125         >

126             <Button 

127             android:id="@+id/bt_submit"

128             android:layout_width="fill_parent"

129             android:layout_height="wrap_content"

130             android:layout_weight="1"

131             android:textStyle="bold"

132             android:textSize="18sp"

133             android:text="@string/btSubmit"

134             />

135             <Button

136                 android:id="@+id/bt_exit"

137                 android:layout_width="fill_parent"

138                 android:layout_height="wrap_content"

139                 android:layout_weight="1"

140                 android:textStyle="bold"

141                 android:textSize="18sp"

142                 android:text="@string/btExit"

143             />   

144     </LinearLayout>

145     

146     <TextView 

147         android:id="@+id/tv_result"

148         android:layout_width="fill_parent"

149         android:layout_height="wrap_content"

150         android:textSize="16sp"

151         android:padding="10dp"

152         android:color="#808000"

153         />

154 

155     <TextView 

156         android:id="@+id/tv_suggest"

157         android:layout_width="fill_parent"

158         android:layout_height="wrap_content"

159         android:textSize="16sp"

160         android:padding="10dp"

161         android:color="#808000"

162         />

163    <TextView 

164         android:id="@+id/tv_target_range"

165         android:layout_width="fill_parent"

166         android:layout_height="wrap_content"

167         android:textSize="16sp"

168         android:padding="10dp"

169         android:color="#808000"

170         />

171 

172   </LinearLayout>

173 </LinearLayout>
View Code

string.xml

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <resources>

 3     <string name="app_name">"BMI Caclator"</string>

 4     <string name="title">"BMI Calculator by Ausk "</string>       

 5     <string name="action_settings">Settings</string>

 6     <string name="height">身高</string>

 7     <string name="myHeight">173</string>

 8     <string name="weight">体重</string>

 9     <string name="myWeight">63</string>

10     <string name="btSubmit">计算BMI</string>

11     <string name="btExit">退出</string>

12     <string name="str_old_result">你的BMI标准值是</string>

13     <string name="str_new_result">新标准值是</string>    

14     <string name="str_Great">Great</string>

15     <string name="str_errorin">輸入有误</string>    

16     <string name="str_unit">单位\:</string>

17     <string name="str_rg1_cms">cms</string>

18     <string name="str_rg1_inchs">inchs</string>

19     <string name="str_rg2_kgs">kgs</string>

20     <string name="str_rg2_lbs">lbs</string>

21     <string name="advice_light">BMI所属范围:BMI&lt;=18.5,偏瘦,多吃点,不要太瘦哦</string>

22     <string name="advice_average">BMI所属范围:18.5&lt;BMI&lt;=24,正常,体型很棒,继续保持!</string>

23     <string name="advice_overweight">BMI所属范围:24&lt;BMI&lt;=28,超重,要多锻炼一下呀</string>

24     <string name="advice_fat">BMI所属范围:BMI&gt;28,肥胖,不要贪吃哦,多锻炼一下</string>

25 </resources>
View Code

 

你可能感兴趣的:(android)