Activity类的onCreateOptionMenu事件方法用来创建选项菜单,该方法定义如下:
@Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }
一般需要将创建选项菜单的代码放在onCreateOptionMenu方法中。通过Menu.add方法可以添加一个选项菜单。该
方法有4种重载形式,定义如下:
public MenuItem add(CharSequence title);
public MenuItem add(int titleRes);
public MenuItem add(int groupId, int itemId, int order, int titleRes);
public MenuItem add(int groupId, int itemId, int order, CharSequence title);
下面代码添加了几个选项菜单:
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) { menu.add(1, 1, 1, "菜单项1"); menu.add(1, 2, 2, "菜单项2"); menu.add(1, 3, 3, "菜单项3"); menu.add(1, 4, 4, "菜单项4"); menu.add(1, 5, 5, "菜单项5"); menu.add(1, 6, 6, "菜单项6"); return true; } }效果图:
从上面的add方法可以看出,该方法返回一个MenuItem对象,每一个MenuItem对象对应一个菜单项。可以通过
MenuItem接口的相应方法来设置与菜单项相关的内容,例如,显示在菜单项上的图像,这个图像需要通过
MenuItem.setIcon方法来添加,该方法的定义如下:
下面代码设置了菜单项的图像:
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) { setIconEnable(menu, true); menu.add(1, 1, 1, "菜单项1").setIcon(R.drawable.b10); menu.add(1, 2, 2, "菜单项2").setIcon(R.drawable.b14); return true; } private void setIconEnable(Menu menu, boolean enable) { try { Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder"); Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class); m.setAccessible(true); m.invoke(menu, enable); } catch (Exception e) { e.printStackTrace(); } } }
这里值得注意的是Android系统4.0以后通过MenuItem对象调用setIcon方法是无法设置菜单项的图标,在Android2.0中
是可以显示,因此想要通过setIcon方法设置图标,可以通过反射将setOptionalIconsVisible方法设置
mOptionalIconsVisible为true。详情参看《解决android4.0系统中菜单(Menu)添加Icon无效问题》
效果图:
Activity的关联我们可以使用MenuItem.setIntent方法指定一个Intent对象即可。setIntent方法的定义如下:
public MenuItem setIntent(Intent intent);
码通过调用setIntent方法来指定某个Activity:
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) { setIconEnable(menu, true); menu.add(1, 1, 1, "菜单项1").setIcon(R.drawable.b10).setIntent(new Intent(MainActivity.this, SecondActivity.class)); return true; } private void setIconEnable(Menu menu, boolean enable) { try { Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder"); Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class); m.setAccessible(true); m.invoke(menu, enable); } catch (Exception e) { e.printStackTrace(); } } }
通过调用MenuItem.setOnMenuItemClickListener方法可以设置菜单项的单击事件,该方法有一个
OnMenuItemClickListener类型的参数,处理菜单项的单击事件类必须实现setOnMenuItemClickListener接口,以下代
码设置了单击事件:
public class MainActivity extends Activity implements OnMenuItemClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { setIconEnable(menu, true); MenuItem item = menu.add(1, 1, 1, "菜单项1").setIcon(R.drawable.b10); item.setOnMenuItemClickListener(this); return true; } @Override public boolean onMenuItemClick(MenuItem item) { if(item.getItemId()==1){ Toast.makeText(MainActivity.this, "点击了菜单项1", Toast.LENGTH_SHORT).show(); } return false; } private void setIconEnable(Menu menu, boolean enable) { try { Class<?> clazz = Class .forName("com.android.internal.view.menu.MenuBuilder"); Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class); m.setAccessible(true); m.invoke(menu, enable); } catch (Exception e) { e.printStackTrace(); } } }
除了设置菜单项的点击事件,还可以使用Activity类的onOptionsItemSelected和onMenuItemSelected方法来响应菜
单项的单击事件,两方法的定义如下:
<pre name="code" class="java">public boolean onOptionsItemSelected(MenuItem item); public boolean onMenuItemSelected(int featureId, MenuItem item);
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) { setIconEnable(menu, true); MenuItem item = menu.add(1, 1, 1, "菜单项1").setIcon(R.drawable.b10); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == 1) { Toast.makeText(MainActivity.this, "点击了菜单项1", Toast.LENGTH_SHORT) .show(); } return true; } private void setIconEnable(Menu menu, boolean enable) { try { Class<?> clazz = Class .forName("com.android.internal.view.menu.MenuBuilder"); Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class); m.setAccessible(true); m.invoke(menu, enable); } catch (Exception e) { e.printStackTrace(); } } }
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) { setIconEnable(menu, true); MenuItem item = menu.add(1, 1, 1, "菜单项1").setIcon(R.drawable.b10); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if (item.getItemId() == 1) { Toast.makeText(MainActivity.this, "点击了菜单项1", Toast.LENGTH_SHORT) .show(); } return true; } private void setIconEnable(Menu menu, boolean enable) { try { Class<?> clazz = Class .forName("com.android.internal.view.menu.MenuBuilder"); Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class); m.setAccessible(true); m.invoke(menu, enable); } catch (Exception e) { e.printStackTrace(); } } }
传统的子菜单是以层次结构显示的,而Android中的子菜单采用了弹出式的显示方式。也就是当单击有子菜单的菜
单项后,父菜单会关闭,而在屏幕上显示的是子菜单。
Menu.addSubMenu方法用来添加子菜单,该方法有4个重载形式,定义如下:
SubMenu addSubMenu(final CharSequence title); SubMenu addSubMenu(final int titleRes); SubMenu addSubMenu(final int groupId, final int itemId, int order, final CharSequence title); SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes);
一个SubMenu对象,SubMenu是Menu的子接口,可以通过SubMenu.add方法添加子菜单项,以下代码设置了两个子
菜单,分别定义了两个菜单的种类:
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) { setIconEnable(menu, true); SubMenu subMenu = menu.addSubMenu(1, 1, 2, "菜单项"); subMenu.setIcon(R.drawable.b10); subMenu.setHeaderIcon(R.drawable.b18); MenuItem menuItem1 = subMenu.add(1, 2, 2, "子菜单1"); /* * 将子菜单1设置成复选框类型 */ menuItem1.setCheckable(true); MenuItem menuItem2 = subMenu.add(2, 3, 3, "子菜单2"); /* * 设置成选项按钮 */ subMenu.setGroupCheckable(2, true, true); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if (item.getItemId() == 2) { Toast.makeText(MainActivity.this, "点击了子菜单项1", Toast.LENGTH_SHORT) .show(); }else if(item.getItemId()==3){ Toast.makeText(MainActivity.this, "点击了子菜单项2", Toast.LENGTH_SHORT) .show(); } return true; } private void setIconEnable(Menu menu, boolean enable) { try { Class<?> clazz = Class .forName("com.android.internal.view.menu.MenuBuilder"); Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class); m.setAccessible(true); m.invoke(menu, enable); } catch (Exception e) { e.printStackTrace(); } } }
效果图:
上下文菜单可以和任意View对象进行关联,例如,TextView、EidtText、Button等控件都可以关联上下文菜单。上
下文菜单的显示效果和子菜单有些类似,也分为菜单头和菜单项。
要想创建上下文菜单,需要覆盖Activity.onCreateContextMenu方法,该方法的定义如下:
<pre name="code" class="java">public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
文菜单项不能带图像,但可以带复选框和选项按钮。以下代码定义了上下文菜单,可以通过长按文本弹出菜单:
public class MainActivity extends Activity { private TextView tv_contextmenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { tv_contextmenu = (TextView) findViewById(R.id.tv_contextmenu); // 注册上下文菜单 registerForContextMenu(tv_contextmenu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if (item.getItemId() == 1) { Toast.makeText(MainActivity.this, "点击了菜单项1", Toast.LENGTH_SHORT) .show(); } else if (item.getItemId() == 2) { Toast.makeText(MainActivity.this, "点击了菜单项2", Toast.LENGTH_SHORT) .show(); } else if (item.getItemId() == 3) { Toast.makeText(MainActivity.this, "点击了子菜单", Toast.LENGTH_SHORT) .show(); } else if (item.getItemId() == 4) { Toast.makeText(MainActivity.this, "点击了子菜单项1", Toast.LENGTH_SHORT) .show(); } else if (item.getItemId() == 5) { Toast.makeText(MainActivity.this, "点击了子菜单项2", Toast.LENGTH_SHORT) .show(); } return true; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("上下文菜单"); menu.setHeaderIcon(R.drawable.b13); menu.add(0, 1, Menu.NONE, "菜单项1"); menu.add(2, 2, Menu.NONE, "菜单项2"); SubMenu sub = menu.addSubMenu(1, 3, Menu.NONE, "子菜单"); sub.add(1, 4, 4, "子菜单项1"); sub.add(1, 5, 5, "子菜单项2"); } }
效果图:
-------------------------------------------------------------------------------------------------------------------------------------------------------
转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/47439377#t6情绪控
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/hai_qing_xu_kong/article/details/47439377