首先創建一個Android Project
在Build target時候我選擇的是Android2.2
然後進入res/values目錄下,創建strings.xml
內容如下:
[xhtml]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">關愛健康:密切關註您無形的財富</string>
<string name="app_name">健康計算器</string>
<string name="bodyHeightString">身高:</string>
<string name="bodyHeightUnitString">厘米</string>
<string name="weightString">體重:</string>
<string name="weightUnitString">公斤</string>
<string name="sexString">性別:</string>
<string name="computeButtonText">計算</string>
<string name="clearButtonText">清零</string>
</resources>
然後創建parameters.xml
[xhtml]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="fontSize">22px</dimen>
<dimen name="TextViewWidth">70px</dimen>
<dimen name="EditTextWidth">120px</dimen>
<dimen name="UnitViewWidth">60px</dimen>
</resources>
然後再進入layout目錄下修改main.xml
[xhtml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:textSize="@dimen/fontSize"
android:text="@string/sexString"
android:id="@+id/sex"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/sex"
android:checkedButton="@+id/radioMan"
android:orientation="horizontal"
android:id="@+id/sexMenu"
>
<RadioButton android:text="男" android:id="@id/radioMan" />
<RadioButton android:text="女" android:id="@+id/radioWoman" />
</RadioGroup>
<!– 身高 –>
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:textSize="@dimen/fontSize"
android:text="@string/bodyHeightString"
android:id="@+id/bodyHeight"
/>
<EditText
android:layout_width="@dimen/EditTextWidth"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/bodyHeight"
android:layout_alignTop="@id/bodyHeight"
android:id="@+id/bodyHeightValue"
/>
<TextView
android:layout_width="@dimen/UnitViewWidth"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/bodyHeightValue"
android:layout_alignTop="@id/bodyHeightValue"
android:textSize="@dimen/fontSize"
android:text="@string/bodyHeightUnitString"
android:id="@+id/bodyHeightUnit"
/>
</RelativeLayout>
<!– 體重 –>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:textSize="@dimen/fontSize"
android:text="@string/weightString"
android:id="@+id/weight"
/>
<EditText
android:layout_width="@dimen/EditTextWidth"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/weight"
android:layout_alignTop="@id/weight"
android:id="@+id/weightValue"
/>
<TextView
android:layout_width="@dimen/UnitViewWidth"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/weightValue"
android:layout_alignTop="@id/weightValue"
android:textSize="@dimen/fontSize"
android:text="@string/weightUnitString"
android:id="@+id/weightUnit"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:text="@string/computeButtonText"
android:id="@+id/computeButton"
/>
<Button
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/computeButton"
android:layout_alignTop="@id/computeButton"
android:text="@string/clearButtonText"
android:id="@+id/clearButton"
/>
</RelativeLayout>
</LinearLayout>
這裡需要註意的一個地方
Android @id和@+id區別?
@id表示引用已經有的id,而@+id表示增加一個id.
然後寫Java文件代碼如下:
[java]
package com.cayden.healthy;
import com.cayden.healthy.R.id;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
public class HealthActity extends Activity {
/** Called when the activity is first created. */
private EditText bodyHeight,weight;
private Button computeButton,clearButton;
private RadioGroup sexRadioGroup;
private static final String large="你已經有些發福瞭,要多運動哦";
private static final String middle="你的體重很標準,繼續保持哦";
private static final String small="有些偏瘦瞭,要多註意營養啊!";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bodyHeight=(EditText)findViewById(R.id.bodyHeightValue);
weight=(EditText)findViewById(R.id.weightValue);
computeButton=(Button)findViewById(R.id.computeButton);
clearButton=(Button)findViewById(R.id.clearButton);
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
computeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double weightDouble=Double.parseDouble((weight.getText().toString()));
double bodyHeightDouble=Double.parseDouble(bodyHeight.getText().toString())/100;
double healthValue=weightDouble/(bodyHeightDouble*bodyHeightDouble) ;
String message="";
if(20<=healthValue&&healthValue<=23){
message=middle;
}else if(healthValue<20){
message=small;
}else{
message=large;
}
new AlertDialog.Builder(HealthActity.this)
.setTitle("計算結果")
.setMessage("健康值: "+(int)healthValue+"(最佳值在20~23之間)"+"/n"
+"說 明: "+message)
.setCancelable(false)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
HealthActity.this.finish();
}
})
.setNegativeButton("繼續", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
}
});
}
}
最後運行效果如圖: