码迷,mamicode.com
首页 > 移动开发 > 详细

Android学习笔记之ContextualMenu上下文菜单用于长按事件的确定

时间:2015-01-17 15:16:03      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:android学习笔记之contextu   contextualmenu   

(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; }
	 */

}


必须长按选项才会弹出对话框以供操作
技术分享

Android学习笔记之ContextualMenu上下文菜单用于长按事件的确定

标签:android学习笔记之contextu   contextualmenu   

原文地址:http://blog.csdn.net/xlgen157387/article/details/42805129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!