禁用 EditText 上下文菜单

Disable EditText context menu(禁用 EditText 上下文菜单)

本文介绍了禁用 EditText 上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为传统的蒙古语制作一个垂直的 EditText.我已经通过在旋转的 ViewGroup 中嵌入稍微修改的 EditText 来成功实现它.我需要创建一个完全自定义的上下文菜单,因为系统不支持垂直文本,并且在旋转 ViewGroup 时也不会旋转.所以我想完全禁用系统上下文菜单.

请注意,这与这些只是试图禁用复制/粘贴/等的问题不同:

  • 在我运行 Android 5.0 的小米 MIUI 手机上,我在所有情况下(光标句柄单击、长按、双击)都会获得上下文菜单.

    更新

    Aritra Roy 的解决方案可以在模拟器、他测试过的其他一些设备以及我的设备上运行.我接受了他的回答,因为它解决了我原来的问题.唯一的负面影响是文本选择也被禁用​​.

    解决方案

    解决方案很简单

    公共类 MainActivity 扩展 AppCompatActivity {编辑文本 et_0;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_0 = findViewById(R.id.et_0);et_0.setCustomSelectionActionModeCallback(new ActionMode.Callback() {@覆盖public boolean onCreateActionMode(ActionMode mode, Menu menu) {//保持文本选择功能可用(选择光标)返回真;}@覆盖public boolean onPrepareActionMode(ActionMode mode, Menu menu) {//防止菜单出现menu.clear();返回假;}@覆盖public boolean onActionItemClicked(ActionMode mode, MenuItem item) {返回假;}@覆盖公共无效 onDestroyActionMode(ActionMode 模式) {}});}}

    I am making a vertical EditText for traditional Mongolian. I have successfully implemented it by embedding a slightly modified EditText inside of a rotated ViewGroup. I need to create a completely custom context menu because the system one does not support vertical text and is also not rotated when the ViewGroup is rotated. So I want to disable the system context menu altogether.

    Note that this is different than these questions that are just trying to disable copy/paste/etc.:

    • How to disable copy/paste from/to EditText
    • EditText: Disable Paste/Replace menu pop-up on Text Selection Handler click event
    • how to disable paste option in android EditText
    • Android: How to TOTALLY disable copy and paste function in Edittext

    Although I don't get the context menu appearing in the simulator, I get it appearing in my Android 5.0.2 Xiaomi phone.

    I have tried:

    • the setCustomSelectionActionModeCallback "solution"
    • the setLongClickable(false); "solution"
    • the onTouchEvent "solution"

    I'm open to hacks but I need it to consistently work across devices. Mark Murphy (a Commons Guy) wrote some time back in reply to another user trying to do something similar:

    I suspect that even if you come up with an answer, it will not work across devices. Device manufacturers have had a tendency to roll their own "context menu" for EditText, defeating developers' attempts to add items into that context menu. My guess is that trying to block that context menu will have similar results.

    Am I out of luck?

    The only thing I can think of now is to completely rewrite TextView and EditText from scratch (well, by modifying the Android source). I know someone else who did something similar, but his code isn't open source. Before I take this major step, I want to try asking for a simpler solution here on Stack Overflow.

    Update: I've been trying modify the TextView source code for the past two days and it looks like a 6 month project. It is a mass of interrelated classes. I need another solution, but I am out of ideas.

    MVCE

    This is the simplest way I could think of to recreate the problem. There is nothing necessary from my custom EditText. The layout has a single EditText made by replacing the default project Hello World's TextView. I changed the min API to 11 to avoid dealing with deprecated methods.

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            EditText editText = (EditText) findViewById(R.id.edit_text);
            editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { return false; }
                @Override
                public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { return false; }
                @Override
                public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { return false; }
                @Override
                public void onDestroyActionMode(ActionMode actionMode) { }
            });
        }
    }
    

    The context menu in the simulator (running API 24) still shows when I click on the cursor handle (but not on a long click or double click). Here is an image:

    On my Xiaomi MIUI phone running Android 5.0, I get the context menu in all situations (cursor handle click, long click, double click).

    Update

    Aritra Roy's solution is working in the simulator, on some other devices that he has tested, and on my device. I have accepted his answer because it solves my original problem. The only negative side effect is that text selection is also disabled.

    解决方案

    the solution is very simple

    public class MainActivity extends AppCompatActivity {
    
    EditText et_0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        et_0 = findViewById(R.id.et_0);
    
        et_0.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                //to keep the text selection capability available ( selection cursor)
                return true;
            }
    
            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                //to prevent the menu from appearing
                menu.clear();
                return false;
            }
    
            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
    
            @Override
            public void onDestroyActionMode(ActionMode mode) {
    
            }
        });
       }
    }
    

    这篇关于禁用 EditText 上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:禁用 EditText 上下文菜单

基础教程推荐