3 import java.text.DecimalFormat;
4
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.TextView;
12
13 public class Bmi extends Activity {
14 /** Called when the activity is first created. */
15 @Override
16 public void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19
20 //Listen for button clicks
21 Button button = (Button)findViewById(R.id.submit);
22 button.setOnClickListener(calcBMI);
23 } 24
25 private OnClickListener calcBMI = new OnClickListener() 26 {
27 public void onClick(View v)
28 {
29 DecimalFormat nf = new DecimalFormat("0.00");
30 EditText fieldheight = (EditText)findViewById(R.id.height);
31 EditText fieldweight = (EditText)findViewById(R.id.weight);
32 double height = Double.parseDouble(fieldheight.getText().toString())/100;
33 double weight = Double.parseDouble(fieldweight.getText().toString());
34 double BMI = weight / (height * height);
35
36 TextView result = (TextView)findViewById(R.id.result);
37 result.setText("Your BMI is "+nf.format(BMI));
38
39 //Give health advice
40 TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
41 if(BMI>25){
42 fieldsuggest.setText(R.string.advice_heavy);
43 }else if(BMI<20){
44 fieldsuggest.setText(R.string.advice_light);
45 }else{
46 fieldsuggest.setText(R.string.advice_average);
47 }
48 }
49 };
50 }
我们会学到:导入其他用到的模组、如何取得界面元件、如何对按钮设定动作。
从上面的完整程序中,我们看到上面介绍到的程序主体都还在,不过也增加了一些内容 。 这些内容即我们的主要程序逻辑。
讲解
//Listen for button clicks
两个反斜线是 java 语言所支援的另一种注解方式。
按钮 代码::::
Button button = (Button)findViewById(R.id.submit);
宣告一个 button 实体,透过 findViewById 方法,从资源档中取得对应的界面元件(按 钮)。这边取出的是"R.id.submit"按钮元件。
"R.id.submit"对应到 XML 描述档的资源是 代码::::
<Button id="@+id/submit"/>
为了确保宣告的型别跟 XML 描述档中描述的界面元件型别一致,好使程序运作正常,
我们在 "findViewById" 方法前加上 "(Button)"修饰,强制将取得的资源型别设成 "button"
型别。
代码::::
button.setOnClickListener(new OnClickListener(){});
这句包含了" button.setOnClickListener"与其中的 "OnClickListener"两个类别。
"setOnClickListener"是 button (按钮)实体的方法。
编辑栏位 代码::::
EditText fieldheight = (EditText)findViewById(R.id.height);
EditText fieldweight = (EditText)findViewById(R.id.weight);
与上面 button 的宣告类似,只是改成宣告 EditText 实体,透过 findViewById 方 法 , 从资源档中取得对应的文字栏位元件。这边取出的是"R.id.height"和"R.id.weight"文字栏位元 件。
运算 代码::::
double height = Double.parseDouble(fieldheight.getText().toString())/100;
double weight = Double.parseDouble(fieldweight.getText().toString());
double BMI = weight / (height * height);
BMI 值的算法是"体重除以身高的平方"。 用计算式来表示,就是 体重(weight) / 身高(height)*身高(height)
这麽看来,上面的程序码就很清晰了。我们先从身高栏位 (fieldheight)、体重栏位 (fieldweight)中取得使用者输入的身高体重数字,再 定义一个双倍精度幅点数(double) 型态 的变数 BMI 来储存运算的结果,因此,BMI 变数中储存了运算出来的实际 BMI 值。
显示结果
我们把 BMI 值运算出来了,接着要将结果显示回屏幕上。
代码::::
TextView result = (TextView)findViewById(R.id.result);
为了将结果显示到屏幕上,在之前 XML 定义档中我们已经预留了一个名为"result"的 TextView 栏位。在程序码中,我们再宣告一个 TextView 实体,透过 findViewById 方法,
从资源档中取得对应的界面元件(文字显示)。这边取出的就是"R.id.result"界面元件。
代码::::
DecimalFormat nf = new DecimalFormat("0.00");
result.setText("Your BMI is "+nf.format(BMI));
透过 java 内建的 DecimalFormat 函式,我们可以将运算结果,以适当的格式显示。透 过"setText"方法,我们可以将指定的字串,显示在屏幕上文字类型的界面元件中。
显示建议
"显示建议"的方式与"显示结果"完全相同,只有多加了几个"if"判断式:当 BMI 值大于 25 显示过重,小于 20 显示过轻。程序码留给读者自习。
代码::::
//Give health advice
TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
if(BMI>25){
fieldsuggest.setText(R.string.advice_heavy);}
else if(BMI<20){
fieldsuggest.setText(R.string.advice_light);
}else{
fieldsuggest.setText(R.string.advice_average);
}
完整的 BMI 程序动作流程,就是使用者在身高体重栏位中输入好身高体重后,按下"
计算 BMI 值"按钮,程序根据识别符号,从对应的身高体重栏位读取输入的值,并做计算,
最后将计算的结果与建议显示到屏幕上。