• Android 自定义EditText实现粘贴,复制,剪切的监听


    package com.dwtedx.qq.view;
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.view.ContextMenu;
    import android.view.MenuItem;
    import android.widget.EditText;
    import android.widget.Toast;
    
    /**
     * An EditText, which notifies when something was cut/copied/pasted inside it.
     * 
     * @author Lukas Knuth
     * @version 1.0
     */
    @SuppressLint("NewApi") public class MyEditText extends EditText implements
            MenuItem.OnMenuItemClickListener {
        private static final int ID_SELECTION_MODE = android.R.id.selectTextMode;
          // Selection context mode
        private static final int ID_SELECT_ALL = android.R.id.selectAll;
        private static final int ID_CUT = android.R.id.cut;
        private static final int ID_COPY = android.R.id.copy;
        private static final int ID_PASTE = android.R.id.paste;
        
        private final Context mContext;
    
        /*
         * Just the constructors to create a new EditText...
         */
        public MyEditText(Context context) {
            super(context);
            this.mContext = context;
        }
    
        public MyEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.mContext = context;
        }
    
        public MyEditText(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            this.mContext = context;
        }
    
        @Override
        protected void onCreateContextMenu(ContextMenu menu) {
        //代码效果,有弹出框选择 粘贴,复制,剪切,类似qq效果..... menu.add(
    0, ID_PASTE, 0, "粘贴").setOnMenuItemClickListener(this); menu.add(0, ID_CUT, 1, "剪切").setOnMenuItemClickListener(this); menu.add(0, ID_COPY, 1, "复制").setOnMenuItemClickListener(this); menu.add(0, ID_SELECT_ALL, 1, "全选").setOnMenuItemClickListener(this); super.onCreateContextMenu(menu); } @Override public boolean onMenuItemClick(MenuItem item) { // TODO Auto-generated method stub return onTextContextMenuItem(item.getItemId()); } @Override public boolean onTextContextMenuItem(int id) { // Do your thing: boolean consumed = super.onTextContextMenuItem(id); // React: switch (id) { case android.R.id.cut: onTextCut(); break; case android.R.id.paste: onTextPaste(); break; case android.R.id.copy: onTextCopy(); } return consumed; } /** * Text was cut from this EditText. */ public void onTextCut() { Toast.makeText(mContext, "Cut!", Toast.LENGTH_SHORT).show(); } /** * Text was copied from this EditText. */ public void onTextCopy() { Toast.makeText(mContext, "Copy!", Toast.LENGTH_SHORT).show(); } /** * Text was pasted into the EditText. */ public void onTextPaste() { Toast.makeText(mContext, "Paste!", Toast.LENGTH_SHORT).show(); } }
  • 相关阅读:
    体验ASP.NET 2.0中的BuildProvider(转载)
    为什么要用非关系数据库?
    Inside ASP.NET 2.0即时编译系统(转载)
    文本信息检索(维基百科)
    通用数据压缩算法简介
    在HttpModule中使用gzip,deflate协议对aspx页面进行压缩
    NoSQL非关系型数据库
    fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786)
    C++中的变量 Variables in C++
    Visual C++, pow(),error C2065: 'pow' : undeclared identifier
  • 原文地址:https://www.cnblogs.com/spring87/p/4357361.html
Copyright © 2020-2023  润新知