Android组件 选项菜单(OptionsMenu)

package com.demo.android.optionsmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
	/**
	 * 单机Menu时,系统会调用此方法
	 * 
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		/**
		 * 参数一:组标识,不分组Menu.NONE
		 * 参数二:菜单项的唯一标识ID
		 * 参数三:菜单项摆放的顺序
		 * 参数四:菜单项显示的文字
		 */
		MenuItem homeMenuItem=menu.add(Menu.NONE,0,0,"主页");
		homeMenuItem.setIcon(R.drawable.home);
		
		MenuItem saveMenuItem=menu.add(Menu.NONE, 1, 1, "保存");
		saveMenuItem.setIcon(R.drawable.save);
		
		MenuItem printMenuItem=menu.add(Menu.NONE, 2, 2, "打印");
		printMenuItem.setIcon(R.drawable.print);
		
		MenuItem searchMenuItem=menu.add(Menu.NONE, 3, 3, "搜索");
		searchMenuItem.setIcon(R.drawable.search);
		
		MenuItem delMenuItem=menu.add(Menu.NONE, 4, 4, "删除");
		delMenuItem.setIcon(R.drawable.del);
		
		MenuItem setMenuItem=menu.add(Menu.NONE, 5, 5, "设置");
		setMenuItem.setIcon(R.drawable.setting);
		
		MenuItem aboutMenuItem=menu.add(Menu.NONE, 6, 6, "关于");
		aboutMenuItem.setIcon(R.drawable.about);
		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		switch(item.getItemId()){
			case 0:{
				Toast.makeText(MainActivity.this, "单击了:"+item.getTitle()+"菜单", Toast.LENGTH_SHORT).show();
				break;
			}
			case 1:{
				Toast.makeText(MainActivity.this, "单击了:"+item.getTitle()+"菜单", Toast.LENGTH_SHORT).show();
				break;
			}
			case 2:{
				Toast.makeText(MainActivity.this, "单击了:"+item.getTitle()+"菜单", Toast.LENGTH_SHORT).show();
				break;
			}
			case 3:{
				Toast.makeText(MainActivity.this, "单击了:"+item.getTitle()+"菜单", Toast.LENGTH_SHORT).show();
				break;
			}
			case 4:{
				Toast.makeText(MainActivity.this, "单击了:"+item.getTitle()+"菜单", Toast.LENGTH_SHORT).show();
				break;
			}
			case 5:{
				Toast.makeText(MainActivity.this, "单击了:"+item.getTitle()+"菜单", Toast.LENGTH_SHORT).show();
				break;
			}
			case 6:{
				Toast.makeText(MainActivity.this, "单击了:"+item.getTitle()+"菜单", Toast.LENGTH_SHORT).show();
				break;
			}
		}
		
		
		
		
		return true;
	}
	
	
}

你可能感兴趣的:(Android组件 选项菜单(OptionsMenu))