Android 菜单
菜单分为两种:系统菜单和上下文菜单。
系统菜单按Menu键出现,通过Activity 的 onCreateOptionsMenu 来创建,onOptionsItemSelected 来响应
上下文菜单是长按控件时出现(如常见的输入法选择), 需为控件单独安装 OnCreateContextMenuListener, 响应则是统一由Activity中的onContextItemSelected 来完成;
[b][color=red]系统菜单 Menu.xml[/color][/b]
[b][color=red]上下文菜单 Context.xml[/color][/b]
其它文档: http://kb.cnblogs.com/page/78304/
菜单分为两种:系统菜单和上下文菜单。
系统菜单按Menu键出现,通过Activity 的 onCreateOptionsMenu 来创建,onOptionsItemSelected 来响应
上下文菜单是长按控件时出现(如常见的输入法选择), 需为控件单独安装 OnCreateContextMenuListener, 响应则是统一由Activity中的onContextItemSelected 来完成;
public class MenuTest extends Activity {
EditText txtInput;
Button btnOK;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.menutest);
//控件初始化
txtInput = (EditText)this.findViewById(R.id.txtInput);
txtInput.setOnCreateContextMenuListener(contextMenuListener);//为控件安装上下文菜单
btnOK = (Button)this.findViewById(R.id.btnOK);
btnOK.setOnCreateContextMenuListener(contextMenuListener);
}
/*
* 创建系统菜单
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//从菜单资源中创建菜单
MenuInflater inflater = this.getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
/*
* 系统菜单响应
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menuOpen:
Toast.makeText(this, "Open", 5).show();
break;
case R.id.menuSave:
Toast.makeText(this, "Save", 5).show();
break;
case R.id.menuExit:
this.finish();
break;
}
return super.onOptionsItemSelected(item);
}
/*
* 创建上下文菜单
*/
OnCreateContextMenuListener contextMenuListener = new OnCreateContextMenuListener(){
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater inflater = MenuTest.this.getMenuInflater();
inflater.inflate(R.menu.context, menu);
//程序方式添加菜单
menu.setHeaderTitle("输入法选择");
menu.add(0, 0, 0, "自定义输入法");
}
};
/*
* 上下文菜单响应
* @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
String message = String.format("您选择了%s!", item.getTitle());
Toast.makeText(this, message, 5).show();
return super.onContextItemSelected(item);
}
}
[b][color=red]系统菜单 Menu.xml[/color][/b]
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/menuGroupMain">
<item android:id="@+id/menuOpen" android:title="Open" android:orderInCategory="1"></item>
<item android:id="@+id/menuSave" android:title="Save" android:orderInCategory="5"></item>
<item android:id="@+id/menuExit" android:title="Exit" android:orderInCategory="3"></item>
</group>
</menu>
[b][color=red]上下文菜单 Context.xml[/color][/b]
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/groupContext" android:checkableBehavior="single">
<item android:title="谷歌拼音" android:id="@+id/menuOption1" android:checkable="true"></item>
<item android:title="搜狗拼音" android:id="@+id/menuOption2" android:checkable="true"></item>
<item android:title="HTC手写" android:id="@+id/menuOption3" ></item>
</group>
</menu>
其它文档: http://kb.cnblogs.com/page/78304/