在TextWatcher中检测退格 [英] Detect backspace in TextWatcher

查看:100
本文介绍了在TextWatcher中检测退格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TextWatcher,并且无法检测到TextWatcher.afterTextChange事件中的 Backspace 键.我还想在textWatcher事件中的某些情况下清除textView.

I am using TextWatcher and I am unable to detect Backspace key in TextWatcher.afterTextChange event. I also want to clear textView on some condition in textWatcher event.

public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    // I want to detect backspace key here
}

推荐答案

KeyListener可以满足您的两个条件.

A KeyListener can fulfil both of your conditions.

mEditText.setOnKeyListener(new OnKeyListener() {                 
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
           if(keyCode == KeyEvent.KEYCODE_DEL){  
             //on backspace
             }
    return false        
        }
});

类似地,在onKey()内,当您想清除textView时,可以放置多个检查语句以检查条件.

Similarly inside the onKey(), you can put multiple check statements to check for the condition, when you would want to clear the textView.

由于 @RankoR 足以指出,请记住 onKeyListener()仅适用于硬件键盘而不是软键盘.

EDIT : As @RankoR was kind enough to point out, please bear in mind that onKeyListener() works only for the hardware keyboards and not the soft keyboards.

这篇关于在TextWatcher中检测退格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆