How to disable copy/paste from/to EditText

后端 未结 24 1309
情歌与酒
情歌与酒 2020-11-22 12:18

In my application, there is a registration screen, where i do not want the user to be able to copy/paste text into the EditText field. I have set an onLon

24条回答
  •  隐瞒了意图╮
    2020-11-22 12:44

    In addition to the setCustomSelectionActionModeCallback, and disabled long-click solutions, it's necessary to prevent the PASTE/REPLACE menus from appearing when the text selection handle is clicked, as per the image below:

    Text selection handle with paste menu

    The solution lies in preventing PASTE/REPLACE menu from appearing in the show() method of the (non-documented) android.widget.Editor class. Before the menu appears, a check is done to if (!canPaste && !canSuggest) return;. The two methods that are used as the basis to set these variables are both in the EditText class:

    • isSuggestionsEnabled() is public, and may thus be overridden.
    • canPaste() is not, and thus must be hidden by introducing a function of the same name in the derived class.

    A more complete answer is available here.

提交回复
热议问题