Android按鍵之Menu詳解

Android手機一般都有三個鍵,返回鍵、Home鍵、菜單鍵;

Android系統的菜單支持主要通過4個接口來實現。

從上圖可以看出Menu是一個父類接口,它下面有兩個子類一個是ContextMenu:它表示是一個上下文菜單,SubMenu表示一個子菜單。它們都可以包含一個或多個的MenuItem菜單項vcD4KPHA+TWVudb3Tv9q2qNLlwcvBvbj2t723qMC0zO2807LLtaXP7rrNzO2809fTssu1paGj0ru49s2ouf1hZGQoKbe9t6jM7bzTssu1pc/uo6xhZGRTdWJNZW51KCm3vbeowLTM7bzT19Oyy7WloaO2+FN1Yk1lbnXT60NvbnRleHRNZW51vMyz0MHLTXVudb3Tv9q1xLe9t6jWrs3io6y7uczhuanBy9K70Km1xLe9t6iho7HIyOfJ6NbDssu1pbXEserM4qGizbyx6rXIOzwvcD4KPHA+PGltZyBzcmM9″/uploadfile/Collfiles/20140310/2014031009165546.png” width=”300″ height=”277″ alt=”\”>

開發菜單和子菜單有兩種方式;一是在Activity重寫的方法onCreateOptionsMenu(Menu menu)方法中調用menu對象添加,然後在重寫Activity的onOptionsItemSelected來響應選擇的菜單項。二是通過資源文件來定義菜單項,然後在onCreateOptionsMenu方法中加載這個xml資源。onCreateOptionsMenu方法在單擊Menu鍵時觸發。

而開發上下文菜單與上類似:分為三個步驟:一是重寫onCreateContextMenu(ContextMenu menu,View source,ContextMenu.MenuInfo info)方法,然後調用registerForContextMenu(View viwe)方法為控件註冊上下文菜單,最後重寫onContextItemSelected(Menuitem item)來響應選擇的菜單項

實例如下:

1、代碼中添加:

package com.android.xiong.memu;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	// 定義菜單項的ItemId
	private final int NEWS = 00;
	private final int COLOR = 01;
	private final int FONT = 02;
	private final int RED = 001;
	private final int BULE = 002;
	private final int GREED = 003;

	private final int BIG = 004;
	private final int SMALL = 005;

	Builder bu;

	Button show;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bu = new AlertDialog.Builder(this);
		show = (Button) findViewById(R.id.context);
		// 為Button註冊上下文菜單
		registerForContextMenu(show);

	}

	// 用戶單擊Menu鍵時觸發
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// 添加新建菜單項
		menu.add(0, NEWS, 0, "新建");
		// 添加顏色子菜單
		SubMenu color = menu.addSubMenu(0, COLOR, 0, "顏色");
		color.setHeaderTitle("選擇顏色");
		color.add(0, RED, 0, "紅色");
		color.add(0, BULE, 0, "藍色");
		color.add(0, GREED, 0, "綠色");
		// 添加字體的菜單
		SubMenu font = menu.addSubMenu(0, FONT, 0, "字體");
		font.setHeaderTitle("選擇字體");
		font.add(0, BIG, 0, "大字體");
		font.add(0, SMALL, 0, "小字體");
		return super.onCreateOptionsMenu(menu);
	}

	// 當用戶單擊菜單項觸發
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case RED:
			bu.setMessage("紅色").setPositiveButton("確定", null).show();
			break;
		case BULE:
			bu.setMessage("藍色").setPositiveButton("確定", null).show();
			break;
		case GREED:
			bu.setMessage("綠色").setPositiveButton("確定", null).show();
			break;
		case BIG:
			bu.setMessage("大號").setPositiveButton("確定", null).show();
			break;
		case SMALL:
			bu.setMessage("小號").setPositiveButton("確定", null).show();
			break;
		case NEWS:
			bu.setMessage("新建").setPositiveButton("確定", null).show();
			break;
		}
		return true;
	}

	// 長按組件響應的上下文菜單
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.add(0, RED, 0, "紅色");
		menu.add(0, BULE, 0, "藍色");
		menu.add(0, GREED, 0, "綠色");
	}

	// 點擊上下文菜單項觸發
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case RED:
			bu.setMessage("紅色").setPositiveButton("確定", null).show();
			break;
		case BULE:
			bu.setMessage("藍色").setPositiveButton("確定", null).show();
			break;
		case GREED:
			bu.setMessage("綠色").setPositiveButton("確定", null).show();
			break;

		}
		return true;
	}

}

2、XML資源定義菜單


    
    
    
    
        
            
            
                
                
                
                
                
            
        
        
    
        
            
            
                
                
                
            
        
        


package com.android.xiong.menu2;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	Button show;
	Builder bu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bu = new AlertDialog.Builder(this);
		show=(Button)findViewById(R.id.bt_show);
		registerForContextMenu(show);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.color_red:
			bu.setMessage("紅色").setPositiveButton("確定", null).show();
			break;
		case R.id.color_bule:
			bu.setMessage("藍色").setPositiveButton("確定", null).show();
			break;
		case R.id.color_greed:
			bu.setMessage("綠色").setPositiveButton("確定", null).show();
			break;
		case R.id.font_big:
			bu.setMessage("大號").setPositiveButton("確定", null).show();
			break;
		case R.id.font_Small:
			bu.setMessage("小號").setPositiveButton("確定", null).show();
			break;
		case R.id.menu_news:
			bu.setMessage("新建").setPositiveButton("確定", null).show();
			break;
		}
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getMenuInflater().inflate(R.menu.main, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.color_red:
			bu.setMessage("紅色").setPositiveButton("確定", null).show();
			break;
		case R.id.color_bule:
			bu.setMessage("藍色").setPositiveButton("確定", null).show();
			break;
		case R.id.color_greed:
			bu.setMessage("綠色").setPositiveButton("確定", null).show();
			break;
		case R.id.font_big:
			bu.setMessage("大號").setPositiveButton("確定", null).show();
			break;
		case R.id.font_Small:
			bu.setMessage("小號").setPositiveButton("確定", null).show();
			break;
		case R.id.menu_news:
			bu.setMessage("新建").setPositiveButton("確定", null).show();
			break;
		}
		return true;
	}
	
	
}

PopMenu表示一個彈出式菜單:使用的的步驟1、創建PopupMenu對象;2、通過MenuInflater的inflate()方法將菜單填充到PopupMenu中3、調用PopupMenu的show()方法顯示彈出菜單

實例:


    
    
        
        
        
    


package com.android.xiong.home;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
	
	Button  show;
	PopupMenu pop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		show=(Button)findViewById(R.id.show);
		setPopupMenu(show);
		show.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				pop.show();
				
			}
		});
		
	}
	
	public void setPopupMenu(View v){
		//創建PopupMenu對象
		pop=new PopupMenu(this, v);
		//將菜單資源加載到Pop中
		getMenuInflater().inflate(R.menu.main,pop.getMenu());
		pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				switch (item.getItemId()) {
				case R.id.red:
					Toast.makeText(MainActivity.this, "紅色", Toast.LENGTH_SHORT).show();
					break;
				case R.id.bule:
					Toast.makeText(MainActivity.this, "藍色", Toast.LENGTH_SHORT).show();
					break;
				case R.id.greed:
					Toast.makeText(MainActivity.this, "綠色", Toast.LENGTH_SHORT).show();
					break;
				}
				return false;
			}
		});
	}

	@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;
	}

}

發佈留言