结合网上资料,文档整理一下自己的理解.
运行效果图:
修改res/menu/main.xml,如下:
ifRoom表示:如果有空间,就显示出来。withText表示:只显示文本.always表示显示在标题栏
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.zsh.demo039.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="@string/action_settings">
<menu>
<item
android:id="@+id/action_settings_open"
android:orderInCategory="100"
android:showAsAction="never"
android:title="开门"/>
<item
android:id="@+id/action_settings_close"
android:orderInCategory="200"
android:showAsAction="never"
android:title="关门"/>
</menu>
</item>
<item
android:id="@+id/start"
android:orderInCategory="200"
android:showAsAction="ifRoom"
android:title="@string/start"/>
<item
android:id="@+id/stop"
android:orderInCategory="300"
android:showAsAction="never"
android:title="@string/stop"/>
<item
android:id="@+id/reset"
android:orderInCategory="400"
android:showAsAction="withText"
android:title="@string/reset">
<menu >
<item
android:id="@+id/reset_video"
android:orderInCategory="100"
android:showAsAction="never"
android:title="重置视频" />
<item
android:id="@+id/reset_music"
android:orderInCategory="100"
android:showAsAction="never"
android:title="重置音乐" />
</menu>
</item>
</menu>
修改MainActivity.java,主要代码:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
/*private final static int Menu_1=Menu.FIRST;
private final static int Menu_2=Menu.FIRST+1;
private final static int Menu_3=Menu.FIRST+2;
private final static int Menu_4=Menu.FIRST+3;
private final static int Menu_5=Menu.FIRST+4;
private final static int Menu_6=Menu.FIRST+5;
private final static int Menu_7=Menu.FIRST+6;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//创建Menu菜单的回调函数
@Override
public boolean onCreateOptionsMenu(Menu menu) {//参数menu是拿到的当前的菜单对象
// Inflate the menu; this adds items to the action bar if it is present.
/*menu.add(0,Menu_1,0,"编辑模式");
menu.add(0,Menu_2,0,"修改壁纸");
menu.add(0,Menu_3,0,"全局搜索");
menu.add(0,Menu_4,0,"桌面缩略图");
menu.add(0,Menu_5,0,"桌面效果");
menu.add(0,Menu_6,0,"系统设置");
menu.add(0,Menu_7,0,"用户信息");
menu.add(0,Menu_1,0,"个人爱好");
//getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);*/
//填充选项菜单
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//Menu选项的回调函数
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
/*int id = item.getItemId();
switch (id) {
case 1:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 6:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 7:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);*/
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.start:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.stop:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.reset:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings_open:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings_close:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.reset_video:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case R.id.reset_music:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
//选项菜单关闭时的回调方法
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
Log.e("onOptionsMenuClosed", "用户菜单关闭了");
}
//菜单显示之前的回调函数
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
Log.e("onPrepareOptionsMenu", "用户菜单准备好被显示了");
return true;
}
}
也可以直接用java编写,只需修改MainActivity.java中的onCreateOptionsMenu(Menu menu)回调函数和onOptionsItemSelected(MenuItem item)回调函数,主要代码:
//Menu菜单的回调函数
@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);
//定义子菜单Menu,然后添加到Menu中
SubMenu fileMenu=menu.addSubMenu("打开文件");
//add(分组id,itemid,排序,"菜单文字")
fileMenu.add(0, Menu_1, 0, "打开文件1");
fileMenu.add(0, Menu_2, 0, "打开文件2");
fileMenu.add(0, Menu_3, 0, "打开文件3");
SubMenu openMenu=menu.addSubMenu("保存文件");
openMenu.add(0, Menu_4, 0, "保存文件1");
openMenu.add(0, Menu_5, 0, "保存文件2");
openMenu.add(0, Menu_6, 0, "保存文件3");
SubMenu saveMenu=menu.addSubMenu("关闭文件");
saveMenu.add(0,Menu_7,0,"关闭文件1");
saveMenu.add(0,Menu_8,0,"关闭文件2");
saveMenu.add(0,Menu_9,0,"关闭文件3");
return super.onCreateOptionsMenu(menu);
}
//选择Menu之后的回调函数
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case 1:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 6:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 7:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 8:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 9:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
default:
//Toast.makeText(this, "item.Length:"+item.getMenuInfo().toString(), Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
运行效果:
也可以给EditText控件注册上下文菜单:
main_activity.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zsh.demo041.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
修改MainActivity.java代码:
package com.zsh.demo041;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et;
private final static int Menu_1=Menu.FIRST;
private final static int Menu_2=Menu.FIRST+1;
private final static int Menu_3=Menu.FIRST+2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.editText1);
//给EditText设置注册上下文菜单,长按事件
registerForContextMenu(et);
}
//创建Context菜单的回调方法
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, view, menuInfo);
//在上下文菜单选项中添加选项内容
menu.add(0, Menu_1, 0, "复制");
menu.add(0, Menu_2, 0, "粘贴");
menu.add(0, Menu_3, 0, "剪切");
}
//ContextMenu菜单选项选择的回调方法
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "您点击了"+item.getTitle().toString(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
@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);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}