时间:2021-01-23 10:12:50 | 栏目:Android代码 | 点击:次
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。