(1)创建选项菜单:重写onCreateOptionsMenu(Menu menu): 1)通过xml设置MenuInflater.inflate(); 2)动态设置;
(2)设置菜单项点击事件:onOptionsItemSelected();
(3)通过xml设置MenuInflater.inflate();
res ——> menu ——>main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_settings1" android:orderInCategory="100" android:showAsAction="never" android:title="菜单一"/> <item android:id="@+id/action_settings2" android:orderInCategory="100" android:showAsAction="never" android:title="菜单二"/> </menu>MainActivity.java
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // 创建选项菜单 // (1)通过xml设置MenuInflater.inflate(); getMenuInflater().inflate(R.menu.main, menu); return true; } // 设置菜单项点击事件:onOptionsItemSelected(); @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.action_settings1: Toast.makeText(this, "点击了菜单项一", Toast.LENGTH_SHORT).show(); break; case R.id.action_settings2: Toast.makeText(this, "点击了菜单项二", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }(4)动态添加菜单项
@Override public boolean onCreateOptionsMenu(Menu menu) { //menu.add(groupId, itemId, order, title):返回值是MenuItem //(2)动态设置 MenuItem menuItem = menu.add(1, 100, 1, "菜单一"); menuItem.setTitle("Menu一"); menuItem.setIcon(R.drawable.ic_launcher); //API>=11时不显示图标 menu.add(1, 101, 2, "菜单二"); menu.add(1, 102, 3, "菜单三"); menu.add(1, 103, 4, "菜单四"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case 100: Intent intent=new Intent(MainActivity.this,Menu1.class); item.setIntent(intent); //设置点击跳转页面 Toast.makeText(this, "点击了菜单项一", Toast.LENGTH_SHORT).show(); break; case 101: Toast.makeText(this, "点击了菜单项二", Toast.LENGTH_SHORT).show(); break; case 102: Toast.makeText(this, "点击了菜单项三", Toast.LENGTH_SHORT).show(); break; case 103: Toast.makeText(this, "点击了菜单项四", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }
(1)创建方法:
1)首先给View(ListView、GridView等)注册上下文菜单registerForContextMenu();
2)添加上下文菜单内容onCreateContextMenu(); A.通过加载xml文件中的菜单项; B.通过代码动态添加;
3)设置菜单点击后的响应事件:onContextItemSelected();
public class MainActivity extends Activity { private ListView listView; private SimpleAdapter simpleAdapter; private List<Map<String, Object>> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); data = new ArrayList<Map<String, Object>>(); simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[] { "image", "text" }, new int[] { R.id.imageView, R.id.textView }); listView.setAdapter(simpleAdapter); // 为ListVIew注册上下文菜单 this.registerForContextMenu(listView); } private List<Map<String, Object>> getData() { Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("image", R.drawable.calendar); map1.put("text", "日历"); data.add(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("image", R.drawable.camera); map2.put("text", "照相机"); data.add(map2); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("image", R.drawable.clock); map3.put("text", "时钟"); data.add(map3); Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("image", R.drawable.games_control); map4.put("text", "游戏"); data.add(map4); Map<String, Object> map5 = new HashMap<String, Object>(); map5.put("image", R.drawable.world); map5.put("text", "地图"); data.add(map5); return data; } //选项菜单 @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub return super.onCreateOptionsMenu(menu); } // 重写上下文菜单 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("应用程序扩展操作"); menu.setHeaderIcon(R.drawable.ic_launcher); //动态添加菜单项 //menu.add(1, 100, 1, "粘贴"); //menu.add(1, 101, 1, "复制"); //menu.add(1, 102, 1, "剪切"); //menu.add(1, 103, 1, "重命名"); //通过XML加载菜单项 MenuInflater inflater=getMenuInflater(); inflater.inflate(R.menu.main_context, menu); } // 设置上下文菜单的点击事件 @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub /*switch (item.getItemId()) { case 100: Toast.makeText(MainActivity.this, "点击了粘贴", Toast.LENGTH_SHORT).show(); break; case 101: Toast.makeText(MainActivity.this, "点击了复制", Toast.LENGTH_SHORT).show(); break; case 102: Toast.makeText(MainActivity.this, "点击了剪切", Toast.LENGTH_SHORT).show(); break; case 103: Toast.makeText(MainActivity.this, "点击了重命名", Toast.LENGTH_SHORT).show(); break; }*/ switch (item.getItemId()) { case R.id.action_settings1: Toast.makeText(MainActivity.this, "点击了粘贴", Toast.LENGTH_SHORT).show(); break; case R.id.action_settings2: Toast.makeText(MainActivity.this, "点击了复制", Toast.LENGTH_SHORT).show(); break; case R.id.action_settings3: Toast.makeText(MainActivity.this, "点击了剪切", Toast.LENGTH_SHORT).show(); break; case R.id.action_settings4: Toast.makeText(MainActivity.this, "点击了重命名", Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } }
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:showAsAction="never" android:title="文件"> <menu > <item android:id="@+id/new_file" android:showAsAction="never" android:title="新建"/> <item android:id="@+id/open_file" android:showAsAction="never" android:title="打开"/> <item android:id="@+id/save_file" android:showAsAction="never" android:title="保存"/> <item android:id="@+id/rename_file" android:showAsAction="never" android:title="重命名"/> </menu> </item> <item android:showAsAction="never" android:title="编辑"> <menu > <item android:id="@+id/v_file" android:showAsAction="never" android:title="粘贴"/> <item android:id="@+id/c_edit" android:showAsAction="never" android:title="复制"/> <item android:id="@+id/x_edit" android:showAsAction="never" android:title="剪切"/> <item android:id="@+id/rename_edit" android:showAsAction="never" android:title="重命名"/> </menu> </item> </menu>MainActivity.java(以下代码将动态添加子菜单注释起来了)
package com.example.testsubmenu; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @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); //动态添加子菜单项 /*SubMenu subMenu1 = menu.addSubMenu("文件"); SubMenu subMenu2 = menu.addSubMenu("编辑"); subMenu1.setHeaderTitle("文件操作"); subMenu1.setHeaderIcon(R.drawable.ic_launcher); subMenu1.add(1, 100, 1, "新建"); subMenu1.add(1, 101, 1, "打开"); subMenu1.add(1, 102, 1, "保存"); subMenu1.add(1, 103, 1, "重命名"); subMenu2.setHeaderTitle("编辑操作"); subMenu2.setHeaderIcon(R.drawable.ic_launcher); subMenu2.add(2, 100, 1, "粘贴"); subMenu2.add(2, 101, 1, "复制"); subMenu2.add(2, 102, 1, "剪切"); subMenu2.add(2, 103, 1, "重命名");*/ //通过加载XML静态加载子菜单 MenuInflater menuInflater=getMenuInflater(); menuInflater.inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub //动态的点击事件 /*if (item.getGroupId()==1) { switch (item.getItemId()) { case 100: Toast.makeText(this, "点击了新建", Toast.LENGTH_SHORT).show(); break; case 101: Toast.makeText(this, "点击了打开", Toast.LENGTH_SHORT).show(); break; case 102: Toast.makeText(this, "点击了保存", Toast.LENGTH_SHORT).show(); break; case 103: Toast.makeText(this, "点击了重命名", Toast.LENGTH_SHORT).show(); break; } } else { switch (item.getItemId()) { case 100: Toast.makeText(this, "点击了粘贴", Toast.LENGTH_SHORT).show(); break; case 101: Toast.makeText(this, "点击了复制", Toast.LENGTH_SHORT).show(); break; case 102: Toast.makeText(this, "点击了剪切", Toast.LENGTH_SHORT).show(); break; case 103: Toast.makeText(this, "点击了重命名", Toast.LENGTH_SHORT).show(); break; } }*/ //静态的点击事件 switch (item.getItemId()) { case R.id.new_file: Toast.makeText(this, "点击了新建", Toast.LENGTH_SHORT).show(); break; case R.id.open_file: Toast.makeText(this, "点击了打开", Toast.LENGTH_SHORT).show(); break; case R.id.save_file: Toast.makeText(this, "点击了保存", Toast.LENGTH_SHORT).show(); break; case R.id.rename_file: Toast.makeText(this, "点击了重命名", Toast.LENGTH_SHORT).show(); break; case R.id.v_file: Toast.makeText(this, "点击了粘贴", Toast.LENGTH_SHORT).show(); break; case R.id.c_edit: Toast.makeText(this, "点击了复制", Toast.LENGTH_SHORT).show(); break; case R.id.x_edit: Toast.makeText(this, "点击了剪切", Toast.LENGTH_SHORT).show(); break; case R.id.rename_edit: Toast.makeText(this, "点击了重命名", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } }