public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo)
{
//这里可以采用扩展Layout文件来添加菜单项
menu.add("First");
menu.add("Second");
super.onCreateContextMenu(menu, v, menuInfo);
}
publicboolean onContextItemSelected(MenuItem item)
{
String itemTitle="Your select context menu item is: "+(String)item.getTitle();
Toast.makeText(getApplicationContext(), itemTitle,Toast.LENGTH_LONG)
.show();
return super.onContextItemSelected(item);
}
{
String [] citys=new String[]{
"BeiJing","ShangHai","GuangZhou","ShengZhen"
};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.menu_items,citys);
setListAdapter(adapter);
}
publicvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ShowListView();
registerForContextMenu(getListView());
}
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv">
</TextView>