2012年4月12日 星期四

[Android]Menu及Toast使用範例



<MenuToast.java>
   1: package com.android.menutoast;
   2:  
   3: import android.app.Activity;
   4: import android.app.AlertDialog;
   5: import android.content.DialogInterface;
   6: import android.os.Bundle;
   7: import android.view.Menu;
   8: import android.view.MenuItem;
   9: import android.widget.Toast;
  10:  
  11: public class MenuToast extends Activity {
  12:     protected static final int MENU_Terry = Menu.FIRST;
  13:     protected static final int MENU_Cathy = Menu.FIRST+1;
  14:     protected static final int MENU_Gary = Menu.FIRST+2;
  15:     protected static final int MENU_Sunny = Menu.FIRST+3;
  16:     protected static final int MENU_Result = Menu.FIRST+4;
  17:     protected static final int MENU_About = Menu.FIRST+5;
  18:     protected static final int MENU_QUIT = Menu.FIRST+6;
  19:     private int cntTerry, cntCathy, cntGary, cntSunny;
  20:     
  21:     /** Called when the activity is first created. */
  22:     @Override
  23:     public void onCreate(Bundle savedInstanceState) {
  24:         super.onCreate(savedInstanceState);
  25:         setContentView(R.layout.main);
  26:     }
  27:     
  28:     @Override
  29:     public boolean onCreateOptionsMenu(Menu menu){
  30:         super.onCreateOptionsMenu(menu);
  31:         menu.add(0,MENU_Terry,0,"Terry");
  32:         menu.add(0,MENU_Cathy,0,"Cathy");
  33:         menu.add(0,MENU_Gary,0,"Gary");
  34:         menu.add(0,MENU_Sunny,0,"Sunny");
  35:         menu.add(0,MENU_Result,0,"投票結果");
  36:         menu.add(0,MENU_About,0,"關於");
  37:         menu.add(0,MENU_QUIT,0,"離開");
  38:         return true;
  39:     }
  40:     
  41:     
  42:     public boolean onOptionsItemSelected(MenuItem item){
  43:         super.onOptionsItemSelected(item);
  44:         switch(item.getItemId()){
  45:         case MENU_Terry:
  46:             cntTerry = cntTerry + 1;
  47:             Toast.makeText(this , "Terry 加 1 票! 已獲得 " + cntTerry + " 票.", Toast.LENGTH_SHORT).show();
  48:             break;
  49:             
  50:         case MENU_Cathy:
  51:             cntCathy = cntCathy + 1;
  52:             Toast.makeText(this , "Cathy 加 1 票! 已獲得 " + cntCathy + " 票.", Toast.LENGTH_SHORT).show();
  53:             break;
  54:             
  55:         case MENU_Gary:
  56:             cntGary = cntGary + 1;
  57:             Toast.makeText(this , "Gary 加 1 票! 已獲得 " + cntGary + " 票.", Toast.LENGTH_SHORT).show();
  58:             break;
  59:             
  60:         case MENU_Sunny:
  61:             cntSunny = cntSunny + 1;
  62:             Toast.makeText(this , "Sunny 加 1 票! 已獲得 " + cntSunny + " 票.", Toast.LENGTH_SHORT).show();
  63:             break;    
  64:  
  65:         case MENU_Result:
  66:             Toast.makeText(this , "投票結果:" + "\nTerry : " + cntTerry + " 票"
  67:                                            + "\nCathy : " + cntCathy + " 票"
  68:                                            + "\nGary  : " + cntGary + " 票"
  69:                                            + "\nSunny : " + cntSunny + " 票"
  70:                                 , Toast.LENGTH_LONG).show();
  71:             break;        
  72:             
  73:         case MENU_About:
  74:             openOptionsDialog();
  75:             break;    
  76:             
  77:         case MENU_QUIT:
  78:             Toast.makeText(this , "感謝使用本程式 !", Toast.LENGTH_LONG).show();
  79:             android.os.Process.killProcess(android.os.Process.myPid());
  80:             break;
  81:         }
  82:         return true;
  83:     }
  84:  
  85:     private void openOptionsDialog() {
  86:         AlertDialog.Builder dialog = new AlertDialog.Builder(MenuToast.this);
  87:         dialog.setTitle("關於  MenuToast 投票系統");
  88:         dialog.setMessage("作者:Terry Wu \n日期:2012/02/28 \ne-mail:terry.wuteyu@gmail.com");
  89:         dialog.setPositiveButton("確定", 
  90:                 new DialogInterface.OnClickListener() {
  91:                     public void onClick(DialogInterface arg0, int arg1) {
  92:  
  93:                     }
  94:                 });
  95:         dialog.show();
  96:     }
  97:     
  98: }

<main.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:     android:layout_width="fill_parent"
   4:     android:layout_height="fill_parent"
   5:     android:orientation="vertical" >
   6:  
   7:     <TextView
   8:         android:id="@+id/textView"
   9:         android:layout_width="wrap_content"
  10:         android:layout_height="wrap_content"
  11:         android:text="@string/prompt_message" />
  12:  
  13: </LinearLayout>

<strings.xml>
   1: <?xml version="1.0" encoding="utf-8"?>
   2: <resources>
   3:  
   4:     <string name="hello">Hello World, MenuToast!</string>
   5:     <string name="app_name">MenuToast</string>
   6:     <string name="prompt_message">【請按 MENU 後,點選人名進行投票 !】</string>
   7:     
   8:  
   9: </resources>

沒有留言:

張貼留言