今天要來撰寫一個簡單計算BMI值的Android App,
這個範例可以練習的基本的Button, TextView應用,
與Button的Click事件,
以下是範例程式碼:
MainActivity.java
package com.example.bmicalculator;
import java.text.DecimalFormat;
import com.example.sqlitetest.R;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button calculateBtn;
private TextView BMIResult;
private EditText heightValue;
private EditText weightValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewIds();
setActionListeners();
}
@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;
}
// 對應元件與其View中的ID
private void findViewIds() {
calculateBtn = (Button)findViewById(R.id.CalculateBtn);
BMIResult = (TextView)findViewById(R.id.BMIResult);
heightValue = (EditText)findViewById(R.id.HeightValue);
weightValue = (EditText)findViewById(R.id.WeightValue);
}
// 設定元件的觸發事件
private void setActionListeners() {
calculateBtn.setOnClickListener(
new OnClickListener() {
@SuppressLint("ShowToast")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double height = Double.parseDouble(heightValue.getText().toString());
double weight = Double.parseDouble(weightValue.getText().toString());
if(checkValue(new double[]{ height, weight })) {
doBMIAction(calculateBMI(height, weight));
} else{
Toast.makeText(v.getContext(), "Invalid value! Please Check values you enter.",
Toast.LENGTH_SHORT).show();
}
}
}
);
}
// 檢查輸入的值是否正確
private boolean checkValue(double[] values) {
boolean isValid = true;
for(int i = 0; i < values.length; i++) {
if(values[i] < 0) {
isValid = false;
break;
}
}
return isValid;
}
// 計算BMI值
private double calculateBMI(double height, double weight) {
double BMI = 0;
BMI = weight / Math.pow((height / 100), 2);
return BMI;
}
// 根據BMI值顯示顏色
private void doBMIAction(double BMI) {
BMIResult.setText((new DecimalFormat("0.0")).format(BMI));
int color;
if(BMI > 24) {
color = Color.RED;
} else if(BMI < 18.5) {
color = Color.BLUE;
} else {
color = Color.GREEN;
}
BMIResult.setTextColor(color);
}
}
activity_main.xml
<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<linearlayout android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical">
<textview android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/HeightText" android:textsize="20dp">
<edittext android:ems="10" android:id="@+id/HeightValue" android:inputtype="numberDecimal" android:layout_height="wrap_content" android:layout_width="match_parent">
<requestfocus>
</requestfocus></edittext>
<textview android:id="@+id/textView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/WeightText" android:textsize="20dp">
<edittext android:ems="10" android:id="@+id/WeightValue" android:inputtype="numberDecimal" android:layout_height="wrap_content" android:layout_width="match_parent">
<button android:id="@+id/CalculateBtn" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Calculate" android:textsize="20dp">
<textview android:id="@+id/BMIResult" android:layout_height="match_parent" android:layout_width="match_parent" android:textsize="50dp">
</textview></button></edittext></textview></textview></linearlayout>
</relativelayout>
留言
張貼留言