实现选项菜单、上下文菜单,以及菜单内部的子菜单。
视图效果:
MainActivity | 选项菜单 | 选项菜单的子菜单 | 上下文菜单(按住按钮或者EditText弹出) |
![]() | ![]() | ![]() | ![]() |
注意:上下文菜单如何弹出?在注册该菜单的视图上按住一段时间后就会弹出菜单。
MainActivity.java
package com.evor.test; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnMenuItemClickListener; import android.view.SubMenu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1 = (Button)findViewById(R.id.btn1); EditText edt1 = (EditText)findViewById(R.id.edt1); //注册上下文菜单(按住该视图一段时间后就会出现menu) registerForContextMenu(btn1); registerForContextMenu(edt1); } //实现选项菜单 @Override public boolean onCreateOptionsMenu(Menu menu) { //参数: groupid,itemid,order,titleRes MenuItem item1 = menu.add(1,1,1,"菜单项1"); MenuItem item2 = menu.add(1,2,2,"菜单项2"); MenuItem item3 = menu.add(1,3,3,"菜单项3"); item3.setIntent(new Intent(this,MainActivity.class)); //设定关联的Activity,点击后会转到该activity MyOnMenuItemClickListener menuItemClickListener = new MyOnMenuItemClickListener(); item1.setOnMenuItemClickListener(menuItemClickListener); item2.setOnMenuItemClickListener(menuItemClickListener); item3.setOnMenuItemClickListener(menuItemClickListener); //添加子菜单 SubMenu fileSubMenu = menu.addSubMenu(1,4,4,"文件"); MenuItem submenuItem1 = fileSubMenu.add(1,5,2,"新建"); MenuItem submenuItem2 = fileSubMenu.add(2,6,3,"打开"); MenuItem submenuItem3 = fileSubMenu.add(2,7,4,"退出"); submenuItem1.setCheckable(true); //设置为多选框 submenuItem2.setChecked(false); //设置是否被勾选 submenuItem3.setChecked(true); //设置是否被勾选 fileSubMenu.setGroupCheckable(2, true, true); //第三参数确定为单选还是多选 return true; } //内部类--选项菜单的菜单项响应类 private class MyOnMenuItemClickListener implements OnMenuItemClickListener{ @Override public boolean onMenuItemClick(MenuItem item) { switch(item.getItemId()){ case 1 : {Toast.makeText(MainActivity.this, "点击了菜单项1", Toast.LENGTH_SHORT).show(); break;} case 2 : {Toast.makeText(MainActivity.this, "点击了菜单项2", Toast.LENGTH_SHORT).show(); break;} case 3 : {Toast.makeText(MainActivity.this, "点击了菜单项3", Toast.LENGTH_SHORT).show(); break;} } return true; } } //上下文菜单的回调函数 @Override public void onCreateContextMenu(ContextMenu menu,View view,ContextMenuInfo menuInfo){ super.onCreateContextMenu(menu, view, menuInfo); menu.setHeaderTitle("上下文菜单"); int itemid = 0; menu.add(0,itemid++, Menu.NONE, "菜单项1"); menu.add(0,itemid++, Menu.NONE, "菜单项2"); menu.add(0,itemid++, Menu.NONE, "菜单项3"); SubMenu subMenu = menu.addSubMenu("子菜单"); subMenu.add("选项1"); subMenu.add("选项2"); } //菜单事件 @Override public boolean onPrepareOptionsMenu(Menu menu){ //在显示选项菜单之前被调用,一般可用来修改即将显示的选项菜单 super.onPrepareOptionsMenu(menu); return true; } @Override public void onOptionsMenuClosed(Menu menu){ //在关闭选项菜单时被调用 super.onOptionsMenuClosed(menu); } @Override public void onContextMenuClosed(Menu menu) { // 在关闭上下文菜单时调用 super.onContextMenuClosed(menu); } @Override public boolean onMenuOpened(int featureId, Menu menu) { //在显示选项菜单之前调用,该方法在onPrepareOptionsMenu方法之后调用 return super.onMenuOpened(featureId, menu); } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.evor.test.MainActivity" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关联菜单的按钮" /> <EditText android:id="@+id/edt1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="关联菜单的编辑框" /> </LinearLayout>