• Android禁止EditText输入框复制粘贴分享


    Android禁止长安EditText时输入框弹出复制粘贴分享选项,原理即

    • 禁止长按事件
    • ActionMode 回调
    • 反射 android.widget.Editor 修改弹框菜单不显示

    Android代码:

    /**
     * 禁止输入框复制粘贴菜单
     */
    public void disableCopyAndPaste(final EditText editText) {
        try {
            if (editText == null) {
                return ;
            }
    
            editText.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });
            editText.setLongClickable(false);
            editText.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        // setInsertionDisabled when user touches the view
                        setInsertionDisabled(editText);
                    }
    
                    return false;
                }
            });
            editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    return false;
                }
    
                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }
    
                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    return false;
                }
    
                @Override
                public void onDestroyActionMode(ActionMode mode) {
    
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void setInsertionDisabled(EditText editText) {
        try {
            Field editorField = TextView.class.getDeclaredField("mEditor");
            editorField.setAccessible(true);
            Object editorObject = editorField.get(editText);
    
            // if this view supports insertion handles
            Class editorClass = Class.forName("android.widget.Editor");
            Field mInsertionControllerEnabledField = editorClass.getDeclaredField("mInsertionControllerEnabled");
            mInsertionControllerEnabledField.setAccessible(true);
            mInsertionControllerEnabledField.set(editorObject, false);
    
            // if this view supports selection handles
            Field mSelectionControllerEnabledField = editorClass.getDeclaredField("mSelectionControllerEnabled");
            mSelectionControllerEnabledField.setAccessible(true);
            mSelectionControllerEnabledField.set(editorObject, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    以上原文地址:https://www.jianshu.com/p/c51a443f2d81

    Kotlin代码:

    import android.view.*
    import android.widget.EditText
    import android.widget.TextView
    import java.lang.reflect.Field
    
    /**
     * Created by Shirley on 2021/8/26 17:13
     */
    object EditTextUtil {
        /**
         * 禁止输入框复制粘贴菜单
         */
        fun disableCopyAndPaste(editText: EditText?) {
            try {
                if (editText == null) {
                    return
                }
                editText.setOnLongClickListener(View.OnLongClickListener { true })
                editText.isLongClickable = false
                editText.setOnTouchListener(View.OnTouchListener { v, event ->
                    if (event.action === MotionEvent.ACTION_DOWN) {
                        // setInsertionDisabled when user touches the view
                        setInsertionDisabled(editText)
                    }
                    false
                })
                editText.customSelectionActionModeCallback = object : ActionMode.Callback {
                    override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                        return false
                    }
    
                    override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
                        return false
                    }
    
                    override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
                        return false
                    }
    
                    override fun onDestroyActionMode(mode: ActionMode?) {}
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    
        private fun setInsertionDisabled(editText: EditText) {
            try {
                val editorField: Field = TextView::class.java.getDeclaredField("mEditor")
                editorField.isAccessible = true
                val editorObject: Any = editorField.get(editText)
    
                // if this view supports insertion handles
                val editorClass = Class.forName("android.widget.Editor")
                val mInsertionControllerEnabledField: Field =
                    editorClass.getDeclaredField("mInsertionControllerEnabled")
                mInsertionControllerEnabledField.isAccessible = true
                mInsertionControllerEnabledField.set(editorObject, false)
    
                // if this view supports selection handles
                val mSelectionControllerEnabledField: Field =
                    editorClass.getDeclaredField("mSelectionControllerEnabled")
                mSelectionControllerEnabledField.isAccessible = true
                mSelectionControllerEnabledField.set(editorObject, false)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    注意ActionMode不要导错类

  • 相关阅读:
    C# WinForm判断Win7下程序是否以管理员身份运行
    C#设置文件(夹)权限
    验证字符串是否为有效的IP地址
    内存对齐
    C#线程启动时传入参数
    写字符串内容到文件
    Thread.Abort() Is Evil.
    如何优雅地控制线程状态
    C#读取文件,返回字符串形式的文件内容
    关于产生不重复随机数的算法 C#
  • 原文地址:https://www.cnblogs.com/Sharley/p/15190739.html
Copyright © 2020-2023  润新知