安卓的EditText删除

安卓的EditText删除

本文介绍了安卓的EditText删除(退格键)键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检测删除(退格键)键事件的EDITTEXT?我已经使用TextWatcher尝试过,但是当EDITTEXT是空的,当我preSS删除键,没有任何反应。我想检测删除键preSS敌人的EDITTEXT即使没有文字。

How can I detect delete (backspace) key event for a editText? I've tried using TextWatcher, but when the editText is empty, when I press delete key, nothing happens. I want to detect delete key press foe an editText even if it has no text.

推荐答案

注:onKeyListener不软键盘工作的

NOTE:onKeyListener doesn't work for soft keyboards

您可以设置 OnKeyListener EDITTEXT 这样你就可以检测到任何按键preSS
编辑:我们正在检查 KeyEvent.KEY code_BACK 退格键一个常见的​​错误,但实际上它是 KeyEvent.KEY code_DEL (真这个名字confusioning!)

You can set OnKeyListener for you editText so you can detect any key press
A common mistake we are checking KeyEvent.KEYCODE_BACK for backspace, but really it is KeyEvent.KEYCODE_DEL (Really that name is confusioning! )

    editText.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
             if(keyCode == KeyEvent.KEYCODE_DEL){
                 //this is for backspace
                 }
        return false;
            }
    });

这篇关于安卓的EditText删除(退格键)键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:02