码迷,mamicode.com
首页 > 其他好文 > 详细

ContextMenu上下文菜单

时间:2015-06-16 18:52:00      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

上下文菜单一般是真针对ListView(多条数据的操作)

  需求:在ListView中显示通话记录的电话号码,长按显示的上下文菜单为复制号码到拨号盘、发送信息、复制号码,与之相对应的事件。

  技术分享

 

布局代码:

  

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical" >
 5 
 6     <ListView
 7         android:id="@+id/listview"
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent" />
10 
11 </LinearLayout>

MainActivity

  1 package com.android.hzy.contextmenu;
  2 import android.app.Activity;
  3 import android.content.Context;
  4 import android.content.Intent;
  5 import android.database.Cursor;
  6 import android.graphics.Color;
  7 import android.net.Uri;
  8 import android.os.Bundle;
  9 import android.provider.CallLog.Calls;
 10 import android.text.ClipboardManager;
 11 import android.view.ContextMenu;
 12 import android.view.ContextMenu.ContextMenuInfo;
 13 import android.view.MenuItem;
 14 import android.view.View;
 15 import android.view.ViewGroup;
 16 import android.widget.AdapterView.AdapterContextMenuInfo;
 17 import android.widget.CursorAdapter;
 18 import android.widget.ListView;
 19 import android.widget.TextView;
 20 
 21 public class MainActivity extends Activity {
 22 
 23     private static final int MENU_DIAL_ID = 0;
 24     private static final int MENU_SMS_ID = 1;
 25     private static final int MENU_COPY_ID = 2;
 26     private ListView listview;
 27     private MyAdapter adapter;
 28     
 29     @Override
 30     protected void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.activity_main);
 33         
 34         listview = (ListView) findViewById(R.id.listview);
 35         Cursor c = getContentResolver().query(Calls.CONTENT_URI, new String[]{Calls._ID,Calls.NUMBER}, null, null, null);
 36         adapter = new MyAdapter(this, c);
 37         // 如果数据是来自于手机本身的数据库   使用CursorAdapter的子类
 38         listview.setAdapter(adapter);
 39         
 40         // 给listview注册上下文菜单
 41         registerForContextMenu(listview);
 42     }
 43 
 44     /**
 45      * 创建上下文菜单
 46      */
 47     @Override
 48     public void onCreateContextMenu(ContextMenu menu, View v,
 49             ContextMenuInfo menuInfo) {
 50         // TODO Auto-generated method stub
 51         menu.add(0, MENU_DIAL_ID, 0, "复制号码到拨号盘");
 52         menu.add(0, MENU_SMS_ID, 0, "发送短信");
 53         menu.add(0, MENU_COPY_ID, 0, "复制号码");
 54         super.onCreateContextMenu(menu, v, menuInfo);
 55     }
 56     
 57     /**
 58      * 上下文菜单被选择
 59      */
 60     @Override
 61     public boolean onContextItemSelected(MenuItem item) {
 62         // TODO Auto-generated method stub
 63         AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) item.getMenuInfo();
 64         int position = acmi.position;
 65         Cursor cursor = (Cursor) adapter.getItem(position);
 66         String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER)); 
 67         
 68         int id = item.getItemId();
 69         Intent intent = null;
 70         switch (id) {
 71         case MENU_DIAL_ID:
 72             intent = new Intent();
 73             intent.setAction(Intent.ACTION_DIAL);
 74             intent.setData(Uri.parse("tel:"+number));
 75             startActivity(intent);
 76             break;
 77             
 78         case MENU_SMS_ID:
 79             intent = new Intent();
 80             intent.setAction(Intent.ACTION_SENDTO);
 81             intent.setData(Uri.parse("smsto:"+number));
 82             startActivity(intent);
 83             break;
 84             
 85         case MENU_COPY_ID:
 86             // 得到剪贴版服务
 87             ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
 88             // 将内容放到剪贴版本中
 89             cm.setText(number);
 90             break;
 91 
 92         default:
 93             break;
 94         }
 95         return super.onContextItemSelected(item);
 96     }
 97     
 98     private final class MyAdapter extends CursorAdapter{
 99 
100         public MyAdapter(Context context, Cursor c) {
101             super(context, c);
102             // TODO Auto-generated constructor stub
103         }
104 
105         // 创建item的布局
106         @Override
107         public View newView(Context context, Cursor cursor, ViewGroup parent) {
108             // TODO Auto-generated method stub
109             TextView tv = new TextView(context);
110             tv.setTextSize(22);
111             tv.setTextColor(Color.WHITE);
112             
113             return tv;
114         }
115 
116         // 绑定数据
117         @Override
118         public void bindView(View view, Context context, Cursor cursor) {
119             // TODO Auto-generated method stub
120             // 得到数据 电话号码
121             String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
122             TextView tv = (TextView) view;
123             tv.setText(number);
124         }
125         
126     }
127 }

注:清单文件中需要添加权限

<uses-permission android:name="android.permission.READ_CONTACTS"/>

 

 

ContextMenu上下文菜单

标签:

原文地址:http://www.cnblogs.com/caoyc/p/4581281.html

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