标签:风飞雪未扬 从零开始学android menu菜单 上下文菜单 安卓子菜单
1
|
public void closeContextMenu()
|
普通
|
|
2
|
public void closeOptionsMenu()
|
普通
|
|
3
|
public boolean onContextItemSelected(MenuItem item)
|
普通
|
|
4
|
public void onContextMenuClosed(Menu menu)
|
普通
|
|
5
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
|
普通
|
|
6
|
public boolean onCreateOptionsMenu(Menu menu)
|
普通
|
当用户选择“Menu”按钮时调用此操作,可以生成一个选项菜单
|
7
|
public boolean onMenuItemSelected(int featureId, MenuItem item)
|
普通
|
|
8
|
public boolean onOptionsItemSelected(MenuItem item)
|
普通
|
当一个选项菜单中的某个菜单项被选中时触发此操作
|
9
|
public void onOptionsMenuClosed(Menu menu)
|
普通
|
|
10
|
public boolean onPrepareOptionsMenu(Menu menu)
|
普通
|
|
11
|
public void openOptionsMenu()
|
普通
|
|
12
|
public MenuInflater getMenuInflater()
|
普通
|
|
13
|
public void registerForContextMenu(View view)
|
普通
|
|
1
|
public static final int FIRST
|
常量
|
|
2
|
public static final int NONE
|
常量
|
|
3
|
public abstract MenuItem add(int groupId, int itemId, int order, CharSequence title)
|
普通
|
此方法用于向菜单之中添加菜单项,参数作用:
|
4
|
public abstract MenuItem add(int groupId, int itemId, int order, int titleRes)
|
普通
|
|
5
|
public abstract SubMenu addSubMenu(int groupId, int itemId, int order, int titleRes)
|
普通
|
|
6
|
public abstract SubMenu addSubMenu(int groupId, int itemId, int order, CharSequence title)
|
普通
|
|
7
|
public abstract void removeGroup(int groupId)
|
普通
|
|
8
|
public abstract void removeItem(int id)
|
普通
|
|
9
|
public abstract void clear()
|
普通
|
|
10
|
public abstract void close()
|
普通
|
|
11
|
public abstract MenuItem getItem(int index)
|
普通
|
|
12
|
public abstract int size()
|
普通
|
|
1
|
public abstract int getGroupId()
|
普通
|
|
2
|
public abstract Drawable getIcon()
|
普通
|
|
3
|
public abstract int getItemId()
|
普通
|
|
4
|
public abstract int getOrder()
|
普通
|
|
5
|
public abstract SubMenu getSubMenu()
|
普通
|
|
6
|
public abstract CharSequence getTitle()
|
普通
|
|
7
|
public abstract boolean isCheckable()
|
普通
|
|
8
|
public abstract boolean isChecked()
|
普通
|
|
9
|
public abstract boolean isEnabled()
|
普通
|
|
10
|
public abstract boolean isVisible()
|
普通
|
|
11
|
public abstract MenuItem setCheckable(boolean checkable)
|
普通
|
|
12
|
public abstract MenuItem setChecked(boolean checked)
|
普通
|
|
13
|
public abstract MenuItem setEnabled(boolean enabled)
|
普通
|
|
14
|
public abstract MenuItem setIcon(Drawable icon)
|
普通
|
|
15
|
public abstract MenuItem setIcon(int iconRes)
|
普通
|
|
16
|
public abstract MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener)
|
普通
|
|
17
|
public abstract MenuItem setTitle(CharSequence title)
|
普通
|
|
18
|
public abstract MenuItem setVisible(boolean visible)
|
普通
|
|
19
|
public abstract ContextMenu.ContextMenuInfo getMenuInfo()
|
普通
|
|
<span style="font-size:18px;">package com.example.menu1; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; 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. // 此方法可以调用menu文件进行配置 // getMenuInflater().inflate(R.menu.main, menu); // add(int groupId, int itemId, int order, Charsequence titleRes) // 菜单组ID itemID 指令指向ID 菜单标题 menu.add(1000, 1, 1, "增加").setIcon(android.R.drawable.ic_menu_add); menu.add(1000, 2, 2, "上传").setIcon( android.R.drawable.ic_menu_upload_you_tube); menu.add(1000, 3, 3, "保存").setIcon(android.R.drawable.ic_menu_save); menu.add(1000, 4, 4, "删除").setIcon(android.R.drawable.ic_menu_delete); menu.add(1000, 5, 5, "详情").setIcon( android.R.drawable.ic_menu_info_details); menu.add(1000, 6, 6, "分享").setIcon(android.R.drawable.ic_menu_share); menu.add(1000, 7, 7, "日期").setIcon( android.R.drawable.ic_menu_my_calendar); return true; } @Override // 菜单选项选中处理事件 public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case 1: Toast.makeText(MainActivity.this, "您点击了增加菜单", 2).show(); break; case 2: Toast.makeText(MainActivity.this, "您点击了上传菜单", 2).show(); break; case 3: Toast.makeText(MainActivity.this, "您点击了保存菜单", 2).show(); break; case 4: Toast.makeText(MainActivity.this, "您点击了删除菜单", 2).show(); break; case 5: Toast.makeText(MainActivity.this, "您点击了详情菜单", 2).show(); break; case 6: Toast.makeText(MainActivity.this, "您点击了分享菜单", 2).show(); break; case 7: Toast.makeText(MainActivity.this, "您点击了日期菜单", 2).show(); break; default: break; } return true; } @Override // 菜单打开时操作 public void openOptionsMenu() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "菜单打开", 2).show(); } @Override // 菜单关闭时操作 public void onOptionsMenuClosed(Menu menu) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "菜单关闭", 2).show(); } @Override // 菜单准备时操作 public boolean onPrepareOptionsMenu(Menu menu) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "准备操作", 2).show(); return true; } } </span>
<span style="font-size:18px;"><menu xmlns:android="http://schemas.android.com/apk/res/android" > <group android:id="@+id/group1" > <item android:id="@+id/item1" android:title="文件"> </item> <item android:id="@+id/item2" android:title="编辑"> </item> <item android:id="@+id/item3" android:title="查询"> </item> <item android:id="@+id/item4" android:title="工程"> </item> <item android:id="@+id/item5" android:title="运行"> </item> <item android:id="@+id/item6" android:title="窗口"> </item> <item android:id="@+id/item7" android:title="帮助"> </item> <item android:id="@+id/item8" android:title="文档"> </item> </group> </menu></span>
<span style="font-size:18px;">package com.example.menu1; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; 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. // 此方法可以调用menu文件进行配置 getMenuInflater().inflate(R.menu.main, menu); return true; } @Override // 菜单选项选中处理事件 public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case 1: Toast.makeText(MainActivity.this, "您点击了增加菜单", 2).show(); break; case 2: Toast.makeText(MainActivity.this, "您点击了上传菜单", 2).show(); break; case 3: Toast.makeText(MainActivity.this, "您点击了保存菜单", 2).show(); break; case 4: Toast.makeText(MainActivity.this, "您点击了删除菜单", 2).show(); break; case 5: Toast.makeText(MainActivity.this, "您点击了详情菜单", 2).show(); break; case 6: Toast.makeText(MainActivity.this, "您点击了分享菜单", 2).show(); break; case 7: Toast.makeText(MainActivity.this, "您点击了日期菜单", 2).show(); break; default: break; } return true; } @Override // 菜单打开时操作 public void openOptionsMenu() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "菜单打开", 2).show(); } @Override // 菜单关闭时操作 public void onOptionsMenuClosed(Menu menu) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "菜单关闭", 2).show(); } @Override // 菜单准备时操作 public boolean onPrepareOptionsMenu(Menu menu) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "准备操作", 2).show(); return true; } } </span>
<span style="font-size:18px;">package com.example.menu2; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private String data[] = { "房泽龙", "22", "河南大学", "教育科学学院", "河南平顶山", "河南开封" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView listView = new ListView(this);//创建ListView listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, data));//绑定Adapter super.registerForContextMenu(listView);//为listview注册菜单 super.setContentView(listView); //设置显示的内容 } @Override//事件处理 public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case 1: Toast.makeText(MainActivity.this, "复制操作", 2).show(); break; case 2: Toast.makeText(MainActivity.this, "详情操作", 2).show(); break; case 3: Toast.makeText(MainActivity.this, "删除操作", 2).show(); break; case 4: Toast.makeText(MainActivity.this, "关闭操作", 2).show(); break; case 5: Toast.makeText(MainActivity.this, "分享操作", 2).show(); break; case 6: Toast.makeText(MainActivity.this, "编辑操作", 2).show(); break; default: break; } return true; } @Override public void onContextMenuClosed(Menu menu) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "菜单关闭", 2).show(); } @Override//增加菜单操作 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub menu.setHeaderTitle("信息操作"); menu.add(1000, 1, 1, "复制").setIcon(android.R.drawable.ic_menu_agenda); menu.add(1000, 2, 2, "详情").setIcon(android.R.drawable.ic_menu_info_details); menu.add(1000, 3, 3, "删除").setIcon(android.R.drawable.ic_menu_delete); menu.add(1000, 4, 4, "关闭").setIcon(android.R.drawable.ic_menu_close_clear_cancel); menu.add(1000, 5, 5, "分享").setIcon(android.R.drawable.ic_menu_share); menu.add(1000, 6, 6, "编辑").setIcon(android.R.drawable.ic_menu_edit); } } </span>
<span style="font-size:18px;"> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:id="@+id/group1"> <item android:id="@+id/a1" android:title="复制"></item> <item android:id="@+id/a2" android:title="详情"></item> <item android:id="@+id/a3" android:title="删除"></item> <item android:id="@+id/a4" android:title="分享"></item> <item android:id="@+id/a5" android:title="发送"></item> </group> </menu> </span>
<span style="font-size:18px;">package com.example.menu3; import android.os.Bundle; import android.app.Activity; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private String data[] = { "房泽龙", "22", "河南大学", "教育科学学院", "河南平顶山", "河南开封" };//数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView listView = new ListView(this);//创建listview listView.setAdapter(new ArrayAdapter<String>(this,//设置adapter android.R.layout.simple_expandable_list_item_1, data)); super.registerForContextMenu(listView);//注册选项菜单 super.setContentView(listView); //设置显示的内容 } @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.a1: Toast.makeText(MainActivity.this, "复制操作", 2).show(); break; case R.id.a2: Toast.makeText(MainActivity.this, "详情操作", 2).show(); break; case R.id.a3: Toast.makeText(MainActivity.this, "删除操作", 2).show(); break; case R.id.a4: Toast.makeText(MainActivity.this, "关闭操作", 2).show(); break; case R.id.a5: Toast.makeText(MainActivity.this, "分享操作", 2).show(); break; case 6: Toast.makeText(MainActivity.this, "编辑操作", 2).show(); break; default: break; } return true; } @Override public void onContextMenuClosed(Menu menu) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "菜单关闭", 2).show(); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.getMenuInflater().inflate(R.menu.main, menu);//将菜单与xml文件中的菜单选项相绑定 super.onCreateContextMenu(menu, v, menuInfo);//创建菜单 menu.setHeaderTitle("信息操作");//设置上下文菜单标题 } } </span>
1
|
public abstract MenuItem getItem()
|
普通
|
得到一个子菜单所属的父菜单对象
|
2
|
public abstract SubMenu setHeaderIcon(int iconRes)
|
普通
|
设置菜单的显示图标
|
3
|
public abstract SubMenu setHeaderTitle(int titleRes)
|
普通
|
设置子菜单的显示标题
|
4
|
public abstract SubMenu setHeaderTitle(CharSequence title)
|
普通
|
设置子菜单的显示标题
|
5
|
public abstract SubMenu setIcon(int iconRes)
|
普通
|
设置每个子菜单项的图标
|
<span style="font-size:18px;">package com.example.menu4; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; public class MainActivity extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { // 显示菜单 SubMenu fileMenu = menu.addSubMenu("文件"); // 子菜单 SubMenu editMenu = menu.addSubMenu("编辑"); // 子菜单 fileMenu.add(1000, 1, 1, "新建") ; // 子菜单项 fileMenu.add(1000, 2, 2, "打开") ; // 子菜单项 fileMenu.add(1000, 3, 3, "保存") ; // 子菜单项 editMenu.add(1000, 4, 4, "撤消"); // 子菜单项 editMenu.add(1000, 5, 5, "恢复"); // 子菜单项 return true ; } @Override public boolean onOptionsItemSelected(MenuItem item) { // 选中某个菜单项 switch (item.getItemId()) { // 判断菜单项ID case 1: Toast.makeText(this, "您选择的是“添加联系人”项。", Toast.LENGTH_LONG).show(); break; case 2: Toast.makeText(this, "您选择的是“查看详情”项。", Toast.LENGTH_LONG).show(); break; case 3: Toast.makeText(this, "您选择的是“删除信息”项。", Toast.LENGTH_LONG).show(); break; case 4: Toast.makeText(this, "您选择的是“另存为”项。", Toast.LENGTH_LONG).show(); break; case 5: Toast.makeText(this, "您选择的是“编辑”项。", Toast.LENGTH_LONG).show(); break; } return false; } } </span>同样,子菜单的选项也可以使用xml文件进行配置
<span style="font-size:18px;"> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:id="@+id/group1"> <item android:id="@+id/item1" android:title="新建"></item> <item android:id="@+id/item2" android:title="打开"></item> <item android:id="@+id/item3" android:title="保存"></item> </group> </menu> </span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <group android:id="@+id/group1"> <item android:id="@+id/item4" android:title="撤销"></item> <item android:id="@+id/item5" android:title="恢复"></item> </group> </menu> </span>
<span style="font-size:18px;">package com.example.menu5; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; public class MainActivity extends Activity { @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.item1: Toast.makeText(MainActivity.this, "新建", 2).show(); break; case R.id.item2: Toast.makeText(MainActivity.this, "打开", 2).show(); break; case R.id.item3: Toast.makeText(MainActivity.this, "保存", 2).show(); break; case R.id.item4: Toast.makeText(MainActivity.this, "撤销", 2).show(); break; case R.id.item5: Toast.makeText(MainActivity.this, "恢复", 2).show(); break; default: break; } return true; } @Override public void onOptionsMenuClosed(Menu menu) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "菜单关闭", 2).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. SubMenu fileMenu = menu.addSubMenu("文件"); SubMenu editMenu = menu.addSubMenu("编辑"); getMenuInflater().inflate(R.menu.file_menu, fileMenu);//将文件子菜单与xml绑定 getMenuInflater().inflate(R.menu.edit_menu, editMenu);//将编辑子菜单与xml绑定 return true; } } </span>
标签:风飞雪未扬 从零开始学android menu菜单 上下文菜单 安卓子菜单
原文地址:http://blog.csdn.net/u013616976/article/details/38820091