(1)在布局文件中创建一个listview:
<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)在res--menu目录下的main.xml文件:
<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文件:
<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)类文件:
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; /** * 上下文菜单适用于listview或者gridview,还必须是长按的时候才会显示 * * @author piaodangdehun * */ public class MainActivity extends Activity { private ListView listView; private ArrayAdapter<String> arrayAdapter; // 适配器用于给listview添加数据 @Override protected 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); // 注册一个contextualMenu上下文菜单 registerForContextMenu(listView); } /* * 添加一个上下文菜单 */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.main, menu); super.onCreateContextMenu(menu, v, menuInfo); } /* * 为适配器添加数值 */ public List<String> getData() { List<String> list = new ArrayList<String>(); for (int i = 0; i < 10; i++) { list.add("jack" + i); } return list; } /* * 上下文菜单的设置 */ @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub AdapterContextMenuInfo 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); } /* * 这是系统自带的menu、注释掉再点击菜单的时候就不会显示系统的菜单 * * @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; } */ }
必须长按选项才会弹出对话框以供操作
时间: 2024-10-17 09:34:34