android 创建菜单列表及其事件

Activity的代码
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items_options_menu, menu);
menu.setHeaderTitle(R.string.options_title);
return;
}

res下menu文件
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/open" android:title="@string/text_open"></item>

<item android:id="@+id/delete" android:title="@string/text_delete"></item>
<item android:id="@+id/share" android:title="@string/share"></item>
<item android:id="@+id/rename" android:title="@string/text_rename"></item>
</menu>

监听事件
public boolean onContextItemSelected(MenuItem item) {

AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
.getMenuInfo();

switch (item.getItemId()) {
             case R.id.open: ... ;
             case R.id.delete: ...


}






你可能感兴趣的:(android)