码迷,mamicode.com
首页 > 移动开发 > 详细

Android-上下文菜单Menu

时间:2018-12-25 22:20:48      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:over   适配   rri   激活   database   board   coding   layout   技术   

上一篇博客介绍了,Android-普通菜单Menu,而这篇博客介绍Android-上下文菜单Menu

 

AndroidManifest.xml 中加入权限:

   <!-- 读取联系人数据的权限 -->
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    
    <!-- 读取通话记录的全身 -->
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    
    <!-- 修改通话记录的权限 -->
    <uses-permission android:name="android.permission.WRITE_CALL_LOG" />

 

Activity

package liudeli.activity;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ContentMenuActivity extends Activity {

    private static final int MENU_ITEM_COPY_TO_DIALER = Menu.FIRST;
    private static final int MENU_ITEM_SEND_SMS = Menu.FIRST + 1;
    private static final int MENU_ITEM_COPY_TO_CLIPBOARD = Menu.FIRST + 2;
    private CursorAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_menu);

        // 初始化控件
        ListView lv = findViewById(R.id.lv);

        // 获取通话记录的数据
        ContentResolver cr = getContentResolver();
        Uri uri = CallLog.Calls.CONTENT_URI;
        String[] projection = new String[]{CallLog.Calls._ID,
                                           CallLog.Calls.CACHED_NAME,
                                           CallLog.Calls.NUMBER,
                                           CallLog.Calls.TYPE};

        Cursor cursor = cr.query(uri, projection, null, null, null);
        adapter = new MyAdapter(this, cursor);
        lv.setAdapter(adapter);

        /**
         * 1 给listview注册上下文菜单
         */
        registerForContextMenu(lv);
    }

    /**
     * 2 创建上下文菜单
     * @param menu
     * @param v
     * @param menuInfo
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(0, MENU_ITEM_COPY_TO_DIALER, 0, "复制号码到拨号盘");
        menu.add(0, MENU_ITEM_SEND_SMS, 0, "发送短信");
        menu.add(0, MENU_ITEM_COPY_TO_CLIPBOARD, 0, "复制号码到粘贴板");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    /**
     * 3 处理上下文菜单的点击事件
     * @param item
     * @return
     */
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = acmi.position;
        Cursor cursor = (Cursor) adapter.getItem(position);
        String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
        Intent intent;
        int id = item.getItemId();
        switch (id) {
            case MENU_ITEM_COPY_TO_DIALER:
                //激活拨号盘的组件,并且把号码传递过去
                intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:"+number));
                startActivity(intent);
                break;
            case MENU_ITEM_SEND_SMS:
                intent = new Intent();
                intent.setAction(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("smsto:"+number));
                startActivity(intent);
                break;
            case MENU_ITEM_COPY_TO_CLIPBOARD:
                //剪贴板是一个系统服务
                ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                //复制数据
                clipboardManager.setText(number);
                break;

            default:
                break;
        }


        return super.onContextItemSelected(item);
    }

    private class MyAdapter extends CursorAdapter {


        private LayoutInflater mInflater;

        public MyAdapter(Context context, Cursor c) {
            super(context, c);
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View view = mInflater.inflate(R.layout.lv_item, null);
            return view;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // 得到控件
            TextView tv_name = view.findViewById(R.id.tv_name);
            TextView tv_number = view.findViewById(R.id.tv_number);
            TextView tv_type = view.findViewById(R.id.tv_type);

            // 得到数据
            String name = cursor.getString(1);
            String number = cursor.getString(2);
            int type = cursor.getInt(3);

            // 绑定数据
            if(name == null){
                tv_name.setText("未知");
            }else{
                tv_name.setText(name);
            }

            tv_number.setText(number);

            switch (type) {
                case CallLog.Calls.INCOMING_TYPE:
                    tv_type.setText("来电");
                    break;
                case CallLog.Calls.OUTGOING_TYPE:
                    tv_type.setText("拨号");

                    break;
                case CallLog.Calls.MISSED_TYPE:
                    tv_type.setText("未接");
                    break;

                default:
                    break;
            }

        }

    }
}

 

Activity的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

 

适配器要使用的 item 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView 
        android:id="@+id/tv_name"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
       
        />
    <TextView 
        android:id="@+id/tv_number"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
       
        />
    <TextView 
        android:id="@+id/tv_type"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
       
        />

</LinearLayout>

 

效果:

技术分享图片

 

Android-上下文菜单Menu

标签:over   适配   rri   激活   database   board   coding   layout   技术   

原文地址:https://www.cnblogs.com/android-deli/p/10176701.html

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