android圖形化程式設計_計算BMI值
第一次在android平臺上做圖形化程式設計,特做筆記,以便加深印象及後續查閱。此程式來自《深入淺出android》
1、 新建工程,file—new—android project,輸入工程名helloandroid01,package名稱為com.android.hello
2、 編輯檢視,開啟helloandroid01—res—layout—main.xml,輸入以下程式碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/height" />
<EditText
android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text="" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/weight" />
<EditText
android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text="" />
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bmi_btn" />
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bmi_result" />
<TextView
android:id="@+id/suggest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
3、 增加字串定義,開啟helloandroid01—res—values—string.xml,輸入以下程式碼
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Helloandroid01Activity!</string>
<string name="app_name">Helloandroid01</string>
<string name="height">身高(cm)</string>
<string name="weight">體重(kg)</string>
<string name="bmi_btn">計算你的bmi值</string>
<string name="bmi_result">你的BMI值是</string>
</resources>
4、 增加建議,新建xml文件,位置在helloandroid01—res—values目錄下,以下為程式碼
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="advice_light">你太輕了!</string>
<string name="advice_average">體形很棒哦!</string>
<string name="advice_heavy">你太肥了!</string>
</resources>
5、 在主文件helloandroid01Activity.java中新增相關程式碼
package com.android.hello;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Helloandroid01Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Listen for button clicks
Button button = (Button)findViewById(R.id.submit);
button.setOnClickListener(calcBMI);
}//end onCreate
private OnClickListener calcBMI = new OnClickListener()
{
public void onClick(View v)
{
DecimalFormat nf = new DecimalFormat("0.00");
EditText fieldheight = (EditText)findViewById(R.id.height);
EditText fieldweight = (EditText)findViewById(R.id.weight);
double height = Double.parseDouble(fieldheight.getText().toString())/100;
double weight = Double.parseDouble(fieldweight.getText().toString());
double BMI = weight/(height*height);
TextView result = (TextView)findViewById(R.id.result);
result.setText("你的BMI值是"+nf.format(BMI));
//Give health advise
TextView fieldsugest = (TextView)findViewById(R.id.suggest);
if(BMI>25)
{
fieldsugest.setText(R.string.advice_heavy);
}
else if(BMI<20)
{
fieldsugest.setText(R.string.advice_light);
}
else
{
fieldsugest.setText(R.string.advice_average);
}
}
};
}
6、 點選run—run,執行結果如下圖所示
收穫:
1. 瞭解了android.widget.Button、EditText、TextView等類
2. 熟悉了類與介面元素之間的對應方法,可以用findViewById方法,例如EditText fieldheight = (EditText)findViewById(R.id.height);
3. 向介面傳輸字串result.setText("你的BMI值是"+nf.format(BMI));
4. 瞭解了變數定義的方法,在main.xml中可以定義,例如android:id="@+id/submit"
5. 瞭解了變數的賦值方法,可以在string.xml或者其他xml檔案中賦值,賦值格式為<string name="height">身高(cm)</string>
6. 瞭解瞭如何新建其他xml檔案的方法
7. 瞭解了變數使用的方法,例如android:text="@string/weight" />
8. 瞭解了按鈕響應函式的方法,先定義button.setOnClickListener(calcBMI);然後新建物件private OnClickListener calcBMI = new OnClickListener();在物件裡定義函式public void onClick(View v),在此函式內輸入具體操作。