The context menu of the ContextualMenu of the Android learning notes is used to determine the long-press event.
(1) create a listview in the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" 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=".MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ListView></RelativeLayout>
(1) main. xml file in the res -- menu directory:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/edit" android:orderInCategory="100" android:showAsAction="never" android:title="@string/edit"/> <item android:id="@+id/share" android:orderInCategory="100" android:showAsAction="never" android:title="@string/share"/> <item android:id="@+id/delete" android:orderInCategory="100" android:showAsAction="never" android:title="@string/delete"/></menu>
(3) string. xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/edit" android:orderInCategory="100" android:showAsAction="never" android:title="@string/edit"/> <item android:id="@+id/share" android:orderInCategory="100" android:showAsAction="never" android:title="@string/share"/> <item android:id="@+id/delete" android:orderInCategory="100" android:showAsAction="never" android:title="@string/delete"/></menu>
(4) class files:
Package com. example. menu_contextualmenu; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. app. activity; import android. view. contextMenu; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. contextMenu. contextMenuInfo; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. adapterView. adapterContextMenuInfo; import android. widget. toast;/*** the context menu is applicable to listview or gridview. The ** @ author piaodangdehun **/public class MainActivity extends Activity {private ListView listView is displayed only when a long press is required; private ArrayAdapter <String> arrayAdapter; // The adapter is used to add data to the listview @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) this. findViewById (R. id. listView1); arrayAdapter = new ArrayAdapter <String> (this, android. r. layout. simple_expandable_list_item_1, getData (); listView. setAdapter (arrayAdapter); // register a contextualMenu context menu registerForContextMenu (listView);}/** Add a context menu */@ Overridepublic void onCreateContextMenu (ContextMenu menu, View v, contextMenuInfo menuInfo) {// TODO Auto-generated method stubgetMenuInflater (). inflate (R. menu. main, menu); super. onCreateContextMenu (menu, v, menuInfo);}/** Add a value for the adapter */public List <String> getData () {List <String> list = new ArrayList <String> (); for (int I = 0; I <10; I ++) {list. add ("jack" + I);} return list;}/** context menu setting */@ Overridepublic boolean onContextItemSelected (MenuItem item) {// TODO Auto-generated method stubAdapterContextMenuInfo info = (AdapterContextMenuInfo) item. getMenuInfo (); switch (item. getItemId () {case R. id. edit: String value = arrayAdapter. getItem (info. position); Toast. makeText (MainActivity. this, "edit .... "+ value, Toast. LENGTH_SHORT ). show (); break; case R. id. delete: Toast. makeText (MainActivity. this, "delete .... ", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onContextItemSelected (item);}/** this is the built-in menu of the system. If you comment out the Menu and click it, the system menu will not be displayed. ** @ Override public boolean onCreateOptionsMenu (menu Menu menu) {// Inflate the * menu; this adds items to the action bar if it is present. * getMenuInflater (). inflate (R. menu. main, menu); return true ;}*/}
The dialog box is displayed only when you press the long button.