亲宝软件园·资讯

展开

Android为ListView创建上下文菜单(ContextMenu) Android编程实现为ListView创建上下文菜单(ContextMenu)的方法

Jacob-wj 人气:0
想了解Android编程实现为ListView创建上下文菜单(ContextMenu)的方法的相关内容吗,Jacob-wj在本文为您仔细讲解Android为ListView创建上下文菜单(ContextMenu)的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ListView,创建,上下文菜单,ContextMenu,下面大家一起来学习吧。

本文实例讲述了Android编程实现为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);
  }
}

希望本文所述对大家Android程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论