首页 > 代码库 > android之OptionsMenu

android之OptionsMenu

首先编写res/layout/Activity_main.xml

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/MyLayout"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >	<TextView 	    android:id="@+id/txt"	    android:layout_width="wrap_content"	    android:layout_height="wrap_content"	    android:text="按下Menu键出现选项菜单"/>    </LinearLayout>

  然后编写Activity类相应的代码

代码如下:

package com.example.myandroid;import android.os.Bundle;import android.app.Activity;import android.app.TabActivity;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.widget.TabHost;import android.widget.TabHost.TabSpec;import android.widget.Toast;public class MainActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {    	menu.add(Menu.NONE,Menu.FIRST+1,5,"删除")    	.setIcon(android.R.drawable.ic_menu_delete);    	    	menu.add(Menu.NONE,Menu.FIRST+2,2,"保存")    	.setIcon(android.R.drawable.ic_menu_save);    	    	menu.add(Menu.NONE,Menu.FIRST+3,6,"帮助")    	.setIcon(android.R.drawable.ic_menu_help);    	    	menu.add(Menu.NONE,Menu.FIRST+4,1,"添加")    	.setIcon(android.R.drawable.ic_menu_add);    	    	menu.add(Menu.NONE,Menu.FIRST+5,4,"详细")    	.setIcon(android.R.drawable.ic_menu_info_details);    	    	menu.add(Menu.NONE,Menu.FIRST+6,7,"发送")    	.setIcon(android.R.drawable.ic_menu_send);    	    	menu.add(Menu.NONE,Menu.FIRST+7,3,"编辑")    	.setIcon(android.R.drawable.ic_menu_edit);    	    	return true;    	    }    @Override    public boolean onOptionsItemSelected(MenuItem item){    	switch(item.getItemId()){    	case Menu.FIRST+1:    		Toast.makeText(this, "delete", Toast.LENGTH_LONG).show();    		break;    	    	case Menu.FIRST+2:    		Toast.makeText(this, "save", Toast.LENGTH_LONG).show();    		break;    	    	case Menu.FIRST+3:    		Toast.makeText(this, "help", Toast.LENGTH_LONG).show();    		break;    	    	case Menu.FIRST+4:    		Toast.makeText(this, "add", Toast.LENGTH_LONG).show();    		break;    	    	case Menu.FIRST+5:    		Toast.makeText(this, "detail", Toast.LENGTH_LONG).show();    		break;    		    	case Menu.FIRST+6:    		Toast.makeText(this, "send", Toast.LENGTH_LONG).show();    		break;    		    	case Menu.FIRST+7:    		Toast.makeText(this, "set", Toast.LENGTH_LONG).show();    		break;    	}    	return false;    }    @Override    public void onOptionsMenuClosed(Menu menu){    	Toast.makeText(this, "选项菜单关闭", Toast.LENGTH_LONG).show();    }    @Override    public boolean onPrepareOptionsMenu(Menu menu){    	Toast.makeText(this, "在菜单显示(onCreateOptionsMenu()方法)之前会调用此操作,可以" +    			"在此操作之中完成一些预处理操作", Toast.LENGTH_LONG).show();    	return true;    }}

  效果如下图:

android之OptionsMenu