首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

替ListView创建ContextMenu

2012-09-07 
为ListView创建ContextMenuContextMenu称为上下文菜单,一般在控件上长按时弹出。今天我们学习ContextMenu的

为ListView创建ContextMenu
ContextMenu称为上下文菜单,一般在控件上长按时弹出。今天我们学习ContextMenu的用法,这里与listview相结合,先在ListView显示几个Item,然后在Item上长按,弹出一个菜单(就是ContextMenu),点击菜单上的项目,提示刚才长按的Item的Position。

main.xml文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent" android:layout_height="fill_parent"    android:orientation="vertical"><TextView android:id="@+id/text" android:text="zzz"android:textSize="18sp" android:textColor="#FFFFFF"android:layout_width="fill_parent" android:layout_height="wrap_content"/><ListView android:id="@+id/list"android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView></LinearLayout>


菜单有3个条目,在/res/menu/test_menu.xml中定义,和OptionMenu差不多,只是没有Icon
package com.hello.menu;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.AdapterView.AdapterContextMenuInfo;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;public class ApiDemos extends Activity {    /** Called when the activity is first created. */private TextView mTextView;private ListView mListView;private String [] values;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mTextView = (TextView) findViewById(R.id.text);        //下面的代码用于初始化一些数据        values = new String[30];        StringBuilder sb = new StringBuilder("z");        for (int i = 0; i < values.length; i++) {        sb.append(i);        values[i] = sb.toString();        }        mListView = (ListView) findViewById(R.id.list);        mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values));        mTextView.setText(String.valueOf(mListView.getAdapter().getCount()));                //为ListView注册ContextMenu        registerForContextMenu(mListView);    }    //创建ContextMenu    @Override    public void onCreateContextMenu(ContextMenu menu, View v,    ContextMenuInfo menuInfo) {    // TODO Auto-generated method stub    if (v.getId() == R.id.list) {    MenuInflater inflater = getMenuInflater();        menu.setHeaderTitle("contextView sample").setHeaderIcon(R.drawable.icon);        inflater.inflate(R.menu.test_menu, menu);    }    super.onCreateContextMenu(menu, v, menuInfo);    }        @Override    public boolean onContextItemSelected(MenuItem item) {    // TODO Auto-generated method stub    if (item.getMenuInfo() instanceof AdapterContextMenuInfo) {    AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();                //处理菜单的点击事件    switch (item.getItemId()) {    case R.id.call:    mTextView.setText(item.getTitle().toString() + menuInfo.position);    break;    case R.id.cc:                        mTextView.setText(item.getTitle().toString() + menuInfo.position);    break;    case R.id.poweroff :                        mTextView.setText(item.getTitle().toString() + menuInfo.position);    break;    }        }    return super.onContextItemSelected(item);    }}

热点排行