--
BMI主頁java
public class Bmi extends Activity {
// 宣告一個常數型別
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST + 1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_ABOUT, 0, "關於...").setIcon(
android.R.drawable.ic_menu_help);
menu.add(0, MENU_QUIT, 0, "結束").setIcon(
android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
// 處理選項動作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ABOUT:
openOptionsDialog();
break;
case MENU_QUIT:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListensers();
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private void findViews() {
button_calc = (Button) findViewById(R.string.submit);
field_height = (EditText) findViewById(R.string.height);
field_weight = (EditText) findViewById(R.string.weight);
}
// Listen for button clicks
private void setListensers() {
button_calc.setOnClickListener(calcBMI);
}
// 傳值到report page
private Button.OnClickListener calcBMI = new Button.OnClickListener() {
public void onClick(View v) {
// "if"判斷式判斷EditText是否有值,String不能用"!= null",equal("")表示null
if ((!(field_height.getText().toString().equals("")))
&& (!(field_weight.getText().toString().equals("")))) {
// 轉換到 report page
Intent intent = new Intent();
intent.setClass(Bmi.this, Report.class);
Bundle bundle = new Bundle();
bundle.putString("KEY_HEIGHT", field_height.getText()
.toString());
bundle.putString("KEY_WEIGHT", field_weight.getText()
.toString());
intent.putExtras(bundle);
startActivity(intent);
} else if ((field_height.getText().toString().equals(""))
|| (field_weight.getText().toString().equals(""))) {
Toast.makeText(Bmi.this, "資料不完整", Toast.LENGTH_LONG).show();
}
}
};
// 對話框
private void openOptionsDialog() {
new AlertDialog.Builder(Bmi.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_msg)
.setPositiveButton(R.string.ok_label,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
}
})
.setNegativeButton(R.string.homepage_label,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
// 開啟網路連結到網頁
Uri uri = Uri
.parse(getString(R.string.homepage_uri));
Intent intent = new Intent(Intent.ACTION_VIEW,
uri);
startActivity(intent);
}
}).show();
}
}
--
BMI的Report頁面
public class Report extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
findViews();
showResults();
setListensers();
}
private Button button_back;
private TextView view_result;
private TextView view_suggest;
private void findViews() {
button_back = (Button) findViewById(R.id.report_back);
view_result = (TextView) findViewById(R.id.result);
view_suggest = (TextView) findViewById(R.id.suggest);
}
// Listen for button clicks
private void setListensers() {
button_back.setOnClickListener(backMain);
}
// 返回計算BMI的主頁
private Button.OnClickListener backMain = new Button.OnClickListener() {
public void onClick(View v) {
// 關閉這個Activity
Report.this.finish();
}
};
// 顯示結果
private void showResults() {
DecimalFormat nf = new DecimalFormat("0.00");
Bundle bundle = this.getIntent().getExtras();
double height = Double.parseDouble(bundle.getString("KEY_HEIGHT")) / 100;
double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));
double BMI = weight / (height * height);
view_result.setText(getString(R.string.result) + nf.format(BMI));
// 給予健康建議
if (BMI > 25) {
showNotification(BMI);
view_suggest.setText(R.string.advice_heavy);
} else if (BMI < 20) {
view_suggest.setText(R.string.advice_light);
} else {
view_suggest.setText(R.string.advice_average);
}
}
// 顯示在上方的訊息提醒
private void showNotification(double BMI) {
NotificationManager barManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification barMsg = new Notification(
// R.drawable.icon
android.R.drawable.stat_sys_warning, "歐歐!你怎麼胖成這樣!",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Bmi.class), PendingIntent.FLAG_UPDATE_CURRENT);
barMsg.setLatestEventInfo(Report.this, "你的 BMI 值過高!", "通知監督人",
contentIntent);
barManager.notify(0, barMsg);
}
}
沒有留言:
張貼留言