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不要导错类

 

posted @ 2021-08-26 17:28  Sharley  阅读(1559)  评论(0编辑  收藏  举报