How to disable copy/paste from/to EditText

后端 未结 24 1669
情歌与酒
情歌与酒 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:45

    Kotlin solution:

    fun TextView.disableCopyPaste() {
        isLongClickable = false
        setTextIsSelectable(false)
        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?) {}
        }
    }
    

    Then you can simply call this method on your TextView:

    override fun onCreate() {
        priceEditText.disableCopyPaste()
    }
    

提交回复
热议问题