Android Learning note contextualmenu context menu for long-press event determination

Source: Internet
Author: User

(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) The 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= "android:showasaction="        never "        android:title=" @string/edit "/>    <item        android:id= "@+id/share"        android:orderincategory= "        " android:showasaction= "Never" android:title= "@string/share"/>    <item        android:id= "@+id/delete" android:orderincategory= "        "        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= "android:showasaction="        never "        android:title=" @string/edit "/>    <item        android:id= "@+id/share"        android:orderincategory= "        " android:showasaction= "Never" android:title= "@string/share"/>    <item        android:id= "@+id/delete" android:orderincategory= "        "        android:showasaction=" never "        android:title=" @string/delete "/></menu>

(4) class file:

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;/** * Context menu applies to ListView or GridView and must be long pressed to display * * @author Piaodangdehun * */public class Mainactiv ity extends Activity {private ListView listview;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 to 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 Settings */@Overridepublic boolean oncontextitemselected (MenuItem Item) {//TODO auto-generated method Stubadapterco Ntextmenuinfo 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 system comes with the menu, commented out and then click on the menu will not display the system's menus * * @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; } */}


You must long press the option to pop up the dialog box for action

Android Learning note contextualmenu context menu for long-press event determination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.