加入選單!
話先說在前頭
- 另外這是一篇筆記,不是一篇教學,所以我不會寫上所有的方式,我只會寫我用的方法。
- 本篇文章是參照[深入淺出 Android -- Google 手持設備應用程式設計入門]跟著做的,所以可能會省略一些說明,詳情請參閱原本的文章。
先看一下新加入的程式部分!
- protected static final int MENU_ABOUT = Menu.FIRST;
- protected static final int MENU_QUIT = Menu.FIRST + 1;
- @Override
- public boolean onCreateOptionsMenu(Menu menu){
- super.onCreateOptionsMenu(menu);
- menu.add(0, MENU_ABOUT, 0, "關於...");
- menu.add(0, MENU_QUIT, 0, "結束");
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item){
- super.onOptionsItemSelected(item);
- switch(item.getItemId()){
- case MENU_ABOUT:
- openOptionsDialog();
- break;
- case MENU_QUIT:
- finish();
- break;
- }
- return true;
- }
當我們要加入選單的時候都會需要去覆載兩個函式,分別是 onCreateOptionsMenu(Menu menu) 跟 onOptionsItemSelected(MenuItem item) 這兩個。
我們在 onCreateOptionsMenu(Menu menu) 中用 menu.add 來增加選單的選項,menu.add 的格式如下,
menu.add(0, 識別符號, 0, 字串或資源識別符號);
[字串或資源識別符號]會顯示在選單上面,所以我們增加了[關於...]跟[結束]兩個選單的選項。
在 onOptionsItemSelected(MenuItem item) 中我們用 switch 來控制按下了什麼按鈕該對應呼叫到什麼事件。
執行程式我們就可以看到按下選單後的出現我們所設定的選單了!
全部的程式如下:
- package com.demo.android.bmi2;
- import android.app.Activity;
- import android.net.Uri;
- import android.os.Bundle;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- //import android.widget.Toast;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import java.text.DecimalFormat;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- public class Bmi2 extends Activity {
- private View btnCalc;
- private TextView fieldHeight;
- private TextView fieldWeight;
- private TextView result;
- private TextView fieldSuggest;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViews();
- setListeners();
- }
- private void setListeners() {
- // TODO Auto-generated method stub
- btnCalc.setOnClickListener(calcBMI);
- }
- private void findViews() {
- // TODO Auto-generated method stub
- btnCalc = (Button)findViewById(R.id.btnCalc);
- fieldHeight = (EditText)findViewById(R.id.etHeight);
- fieldWeight = (EditText)findViewById(R.id.etWeight);
- result = (TextView)findViewById(R.id.tvResult);
- fieldSuggest = (TextView)findViewById(R.id.tvSuggest);
- }
- private OnClickListener calcBMI = new OnClickListener()
- {
- public void onClick(View v)
- {
- DecimalFormat nf = new DecimalFormat("0.00");
- double height = Double.parseDouble(fieldHeight.getText().toString())/100;
- double weight = Double.parseDouble(fieldWeight.getText().toString());
- double bmi = weight / (height * height);
- result.setText("Your BMI is " + nf.format(bmi));
- 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_avg);
- }
- //openOptionsDialog();
- }
- };
- private void openOptionsDialog() {
- // TODO Auto-generated method stub
- //Toast.makeText(Bmi2.this, "BMI2 計算器", Toast.LENGTH_SHORT).show();
- new AlertDialog.Builder(Bmi2.this)
- .setTitle("關於 Android BMI2")
- .setMessage("Android BMI2 Calc")
- .setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- })
- .setNegativeButton(R.string.homepage_label,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Uri uri = Uri.parse("http://androidbmi.googlecode.com/");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- })
- .show();
- }
- protected static final int MENU_ABOUT = Menu.FIRST;
- protected static final int MENU_QUIT = Menu.FIRST + 1;
- @Override
- public boolean onCreateOptionsMenu(Menu menu){
- super.onCreateOptionsMenu(menu);
- menu.add(0, MENU_ABOUT, 0, "關於...");
- menu.add(0, MENU_QUIT, 0, "結束");
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item){
- super.onOptionsItemSelected(item);
- switch(item.getItemId()){
- case MENU_ABOUT:
- openOptionsDialog();
- break;
- case MENU_QUIT:
- finish();
- break;
- }
- return true;
- }
- }
沒有留言:
張貼留言