How to disable copy/paste from/to EditText

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

    i added Extension Function in Kotlin language :

    fun EditText.disableTextSelection() {
        this.setCustomSelectionActionModeCallback(object : android.view.ActionMode.Callback {
            override fun onActionItemClicked(mode: android.view.ActionMode?, item: MenuItem?): Boolean {
                return false
            }
            override fun onCreateActionMode(mode: android.view.ActionMode?, menu: Menu?): Boolean {
                return false
            }
            override fun onPrepareActionMode(mode: android.view.ActionMode?, menu: Menu?): Boolean {
                return false
            }
            override fun onDestroyActionMode(mode: android.view.ActionMode?) {
            }
        })
    }
    

    you can use it like this :

    edit_text.disableTextSelection()
    

    also added below line in your xml :

                    android:longClickable="false"
                    android:textIsSelectable="false"
    

提交回复
热议问题