• Android-上下文菜单Menu


    上一篇博客介绍了,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>

    效果:

  • 相关阅读:
    bzoj1059: [ZJOI2007]矩阵游戏
    NEW
    bzoj2438: [中山市选2011]杀人游戏
    bzoj4554: [Tjoi2016&Heoi2016]游戏 二分图匹配
    【高精度】模板 (C++)
    【BZOJ4025】二分图 LCT
    读入/输出模板
    一些 Markdown 语法
    题解 P3732 [HAOI2017]供给侧改革
    题解 CF1598A Computer Game
  • 原文地址:https://www.cnblogs.com/android-deli/p/10176701.html
Copyright © 2020-2023  润新知