阅读《Android 从入门到精通》(27)——选项菜单

选项菜单(OptionMenu)

使用 Menu 按键触发的菜单叫做选项菜单

android.R.drawable 属性

阅读《Android 从入门到精通》(27)——选项菜单_第1张图片
阅读《Android 从入门到精通》(27)——选项菜单_第2张图片
阅读《Android 从入门到精通》(27)——选项菜单_第3张图片

OptionMenu 示例

完整工程:http://download.csdn.net/detail/sweetloveft/9436986

本程序主要学习 OptionMenu 的使用,需要关注的是 MainActivity.java 里面的 onCreateOptionsMenu、onPrepareOptionsMenu、OnOptionsItemSelected、onOptionsMenuClosed 的响应,此外需要注意 Menu 的 add 方法。

1.MainActivity.java

package com.sweetlover.activity;

import com.sweetlover.optionmenu.R;

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 {

	private MenuItem miDelete = null;
	private MenuItem miSave = null;
	private MenuItem miHelp = null;
	private MenuItem miAdd = null;
	private MenuItem miDetail = null;
	private MenuItem miSend = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		miDelete = menu.add(Menu.NONE, Menu.FIRST + 1, 1, "删除");
		miDelete.setIcon(android.R.drawable.ic_menu_delete);
		miSave = menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存");
		miSave.setIcon(android.R.drawable.ic_menu_save);
		miHelp = menu.add(Menu.NONE, Menu.FIRST + 3, 3, "帮助");
		miHelp.setIcon(android.R.drawable.ic_menu_help);
		miAdd = menu.add(Menu.NONE, Menu.FIRST + 4, 4, "添加");
		miAdd.setIcon(android.R.drawable.ic_menu_add);
		miDetail = menu.add(Menu.NONE, Menu.FIRST + 5, 5, "详细");
		miDetail.setIcon(android.R.drawable.ic_menu_info_details);
		miSend = menu.add(Menu.NONE, Menu.FIRST + 6, 6, "发送");
		miSend.setIcon(android.R.drawable.ic_menu_send);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Toast.makeText(MainActivity.this, "删除菜单被点击了!", Toast.LENGTH_SHORT)
					.show();
			break;
		case Menu.FIRST + 2:
			Toast.makeText(MainActivity.this, "保存菜单被点击了!", Toast.LENGTH_SHORT)
					.show();
			break;
		case Menu.FIRST + 3:
			Toast.makeText(MainActivity.this, "帮助菜单被点击了!", Toast.LENGTH_SHORT)
					.show();
			break;
		case Menu.FIRST + 4:
			Toast.makeText(MainActivity.this, "添加菜单被点击了!", Toast.LENGTH_SHORT)
					.show();
			break;
		case Menu.FIRST + 5:
			Toast.makeText(MainActivity.this, "详细菜单被点击了!", Toast.LENGTH_SHORT)
					.show();
			break;
		case Menu.FIRST + 6:
			Toast.makeText(MainActivity.this, "发送菜单被点击了!", Toast.LENGTH_SHORT)
					.show();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onOptionsMenuClosed(Menu menu) {
		// TODO Auto-generated method stub
		super.onOptionsMenuClosed(menu);
		Toast.makeText(MainActivity.this, "选项菜单关闭了!", Toast.LENGTH_SHORT)
				.show();
	}
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="20sp"
        android:text="@string/tittle" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="20sp"
        android:text="@string/note" />

</LinearLayout>

3.string.xml

<resources>

    <string name="app_name">OptionMenu</string>
    <string name="tittle">Option Menu Application</string>
    <string name="note">单击 Menu 键</string>

</resources>

你可能感兴趣的:(android)