菜单(四)给ListView的item添加上下文菜单

时间:2023-01-20 16:49:50
<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="com.example.contextmenu.MainActivity" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>
package com.example.contextmenu;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.ContextMenu;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.view.ContextMenu.ContextMenuInfo;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends Activity {private ListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) findViewById(R.id.listView1);ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,getData());listView.setAdapter(arrayAdapter);registerForContextMenu(listView);//给listview注册上下文菜单}public List<String> getData(){List<String> list = new ArrayList<String>();for(int i = 1; i < 10; i++){list.add("list " + i);}return list; }@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {// 加载xml中的上下文菜单super.onCreateContextMenu(menu, v, menuInfo);MenuInflater menuInflater = getMenuInflater();menuInflater.inflate(R.menu.main, menu);}@Overridepublic boolean onContextItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.edit:Toast.makeText(MainActivity.this, "编辑操作", Toast.LENGTH_SHORT).show();break;case R.id.share:Toast.makeText(MainActivity.this, "分享操作", Toast.LENGTH_SHORT).show();break;case R.id.delete:Toast.makeText(MainActivity.this, "删除操作", Toast.LENGTH_SHORT).show();break;default:break;}return super.onContextItemSelected(item);}}

res/menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.contextmenu.MainActivity" >

<item
android:id="@+id/edit"
android:orderInCategory="100"
android:showAsAction="never"
android:title="编辑"/>
<item
android:id="@+id/share"
android:orderInCategory="100"
android:showAsAction="never"
android:title="分享"/>
<item
android:id="@+id/delete"
android:orderInCategory="100"
android:showAsAction="never"
android:title="删除"/>

</menu>
菜单(四)给ListView的item添加上下文菜单菜单(四)给ListView的item添加上下文菜单菜单(四)给ListView的item添加上下文菜单