上下文菜单一般是真针对ListView(多条数据的操作)
需求:在ListView中显示通话记录的电话号码,长按显示的上下文菜单为复制号码到拨号盘、发送信息、复制号码,与之相对应的事件。
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </LinearLayout>
MainActivity
package com.android.hzy.contextmenu;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.text.ClipboardManager;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity { private static final int MENU_DIAL_ID = 0;
private static final int MENU_SMS_ID = 1;
private static final int MENU_COPY_ID = 2;
private ListView listview;
private MyAdapter adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listview);
Cursor c = getContentResolver().query(Calls.CONTENT_URI, new String[]{Calls._ID,Calls.NUMBER}, null, null, null);
adapter = new MyAdapter(this, c);
// 如果数据是来自于手机本身的数据库 使用CursorAdapter的子类
listview.setAdapter(adapter); // 给listview注册上下文菜单
registerForContextMenu(listview);
} /**
* 创建上下文菜单
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.add(0, MENU_DIAL_ID, 0, "复制号码到拨号盘");
menu.add(0, MENU_SMS_ID, 0, "发送短信");
menu.add(0, MENU_COPY_ID, 0, "复制号码");
super.onCreateContextMenu(menu, v, menuInfo);
} /**
* 上下文菜单被选择
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) item.getMenuInfo();
int position = acmi.position;
Cursor cursor = (Cursor) adapter.getItem(position);
String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER)); int id = item.getItemId();
Intent intent = null;
switch (id) {
case MENU_DIAL_ID:
intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
break; case MENU_SMS_ID:
intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:"+number));
startActivity(intent);
break; case MENU_COPY_ID:
// 得到剪贴版服务
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 将内容放到剪贴版本中
cm.setText(number);
break; default:
break;
}
return super.onContextItemSelected(item);
} private final class MyAdapter extends CursorAdapter{ public MyAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
} // 创建item的布局
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv = new TextView(context);
tv.setTextSize(22);
tv.setTextColor(Color.WHITE); return tv;
} // 绑定数据
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
// 得到数据 电话号码
String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
TextView tv = (TextView) view;
tv.setText(number);
} }
}
注:清单文件中需要添加权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>