Android EditText删除(退格)按键事件
如何检测 editText 的删除(退格)键事件?我尝试过使用 TextWatcher,但是当 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
注意:
onKeyListener
不适用于软键盘。您可以为
editText
设置OnKeyListener
,以便检测任何按键编辑:一个常见的错误是我们检查
KeyEvent.KEYCODE_BACK
是否有backspace
,但实际上它是KeyEvent.KEYCODE_DEL
(这个名字真的很令人困惑! )NOTE:
onKeyListener
doesn't work for soft keyboards.You can set
OnKeyListener
for youeditText
so you can detect any key pressEDIT: A common mistake we are checking
KeyEvent.KEYCODE_BACK
forbackspace
, but really it isKeyEvent.KEYCODE_DEL
(Really that name is very confusing! )你问这个问题已经有一段时间了,但我也遇到了同样的问题。正如埃斯特尔已经提到的,关键监听器的问题是它们只能使用硬件键盘。要使用IME(软键盘)来完成此操作,解决方案要复杂一些。
我们实际想要重写的唯一方法是
EditText
的InputConnection
类中的sendKeyEvent
。当 IME 中发生按键事件时调用此方法。但为了覆盖它,我们需要实现一个自定义的EditText
,它覆盖onCreateInputConnection
方法,将默认的InputConnection
对象包装在代理类中! :|听起来很复杂,但这是我可以设计的最简单的示例:
调用
setRandomBackgroundColor
的行是我的特殊退格操作发生的地方。在本例中,更改EditText
的背景颜色。如果您从 XML 中扩充它,请记住使用完整的包名称作为标签:
It's been a while since you asked but I just had the same issue. As already mentioned by Estel the problem with key listeners is that they only work with hardware keyboards. To do this with an IME (soft keyboard), the solution is a bit more elaborate.
The single method we actually want to override is
sendKeyEvent
in theEditText
'sInputConnection
class. This method is called when key events occur in an IME. But in order to override this, we need to implement a customEditText
which overrides theonCreateInputConnection
method, wrapping the defaultInputConnection
object in a proxy class! :|Sounds complicated, but here's the simplest example I could contrive:
The line with the call to
setRandomBackgroundColor
is where my special backspace action occurs. In this case, changing theEditText
's background colour.If you're inflating this from XML remember to use the full package name as the tag:
这只是对 Idris 答案的补充,还添加了对 deleteSurroundingText 的覆盖。我在这里找到了更多信息: Android:WebView/BaseInputConnection 中的退格键
This is just an addition to Idris's answer, adding in the override to deleteSurroundingText as well. I found more info on that here: Android: Backspace in WebView/BaseInputConnection
这是我的简单解决方案,适用于所有 API:
UPDATE 17.04.18 。
正如评论中指出的,如果 EditText 为空,此解决方案不会跟踪退格键(与大多数其他解决方案相同)。
不过,这对于大多数用例来说已经足够了。
PS 如果我今天必须创建类似的东西,我会这样做:
然后将其用作常规 TextWatcher:
Here is my easy solution, which works for all the API's:
UPDATE 17.04.18 .
As pointed out in comments, this solution doesn't track the backspace press if EditText is empty (the same as most of the other solutions).
However, it's enough for most of the use cases.
P.S. If I had to create something similar today, I would do:
Then just use it as a regular TextWatcher:
我花了 2 天时间寻找解决方案,并找到了一个可行的解决方案:)(在软键上)
将文本观察器添加到您的 EditText 后:
我希望它也适用于其他 Android 设备(三星、LG 等)。
I sent 2 days to find a solution and I figured out a working one :) (on soft keys)
After add the textwatcher to your EditText:
I hope it works on another android devices too (samsung, LG, etc).
我的简单解决方案效果很好。您应该添加一个标志。我的代码片段:
My simple solution which works perfectly. You should to add a flag. My code snippet:
创建EditText的示例
使用TextWatcher自定义TextWatcher
Example of creating EditText with TextWatcher
custom TextWatcher
对于使用 Kotlin 的人
addOnTextChanged
不够灵活来处理某些情况(例如:在编辑文本为空时检测用户是否按删除键)setOnkeyListener
甚至可以在软键盘或硬键盘上使用!但仅在某些设备上。就我而言,它适用于三星 s8,但不适用于小米 mi8 se。如果你使用kotlin,你可以使用crossline函数
doOnTextChanged
,它与addOnTextChanged
相同,但即使编辑文本为空也会触发回调。注意:doOnTextChanged 是 Android KTX 库 的一部分
for some one who's using Kotlin
addOnTextChanged
is not flexible enought to handle some cases (ex: detect if user press delete when edit text was empty)setOnkeyListener
worked even soft keyboard or hardkeyboard! but just on some devices. In my case, it work on Samsung s8 but not work on Xiaomi mi8 se.if you using kotlin, you can use crossline function
doOnTextChanged
, it's the same asaddOnTextChanged
but callback is triggered even edit text was empty.NOTE: doOnTextChanged is a part of Android KTX library
基于 @Jiff
ZanyEditText
这里是WiseEditText
和setSoftKeyListener(OnKeyListener)
Based on @Jiff
ZanyEditText
here isWiseEditText
withsetSoftKeyListener(OnKeyListener)
这似乎对我有用:
This seems to be working for me :
我在对话框中也遇到了同样的问题..因为我正在使用setOnKeyListener..但我设置了默认返回true。更改如下代码后,它对我来说工作正常..
I am also faced same issue in Dialog.. because I am using setOnKeyListener.. But I set default return true. After change like below code it working fine for me..
我的问题是,我有自定义
Textwatcher
,所以我不想将OnKeyListener
添加到EditText
以及我没有想要创建自定义EditText
。我想检测afterTextChanged
方法中是否按下了退格键,因此我不应该触发我的事件。这就是我解决这个问题的方法。希望这对某人有帮助。
My problem was, that I had custom
Textwatcher
, so I didn't want to addOnKeyListener
to anEditText
as well as I didn't want to create customEditText
. I wanted to detect if backspace was pressed in myafterTextChanged
method, so I shouldn't trigger my event.This is how I solved this. Hope it would be helpful for someone.
我已经在 4.2、4.4、6.0 版本上测试了 @Jeff 的解决方案。在 4.2 和 6.0 上,它运行良好。但在 4.4 上,它不起作用。
我找到了一个简单的方法来解决这个问题。关键是在EditText内容的开头插入一个不可见的字符,并且不要让用户将光标移动到该字符之前。我的方法是插入一个空白字符,其上有一个零宽度的 ImageSpan。这是我的代码。
我们需要自定义一个具有 SelectionChangeListener 的 EditText
}
最后一步
现在,我们就完成了~当 EditText 没有实际内容时,我们可以检测退格键事件,用户对我们的技巧一无所知。
I have tested @Jeff's solution on version 4.2, 4.4, 6.0. On 4.2 and 6.0, it works well. But on 4.4, it doesn't work.
I found an easy way to work around this problem. The key point is to insert an invisible character into the content of EditText at the begining, and don't let user move cursor before this character. My way is to insert a white-space character with an ImageSpan of Zero Width on it. Here is my code.
And we need custom an EditText which has a SelectionChangeListener
}
And the last step
And now, we are done~ We can detect backspace key event when EditText has no actual content, and user will know nothing about our trick.
这个问题可能很老了,但使用 TextWatcher 答案非常简单。
This question may be old but the answer is really simple using a TextWatcher.
虽然迟来了,但它可能会对新访问者有所帮助,使用
TextWatcher()
会很有帮助,而且它也适用于软键盘和硬键盘。Belated but it may help new visitors, use
TextWatcher()
instead will help alot and also it will work for both soft and hard keyboard as well.您可以在活动上设置一个关键侦听器,并在回调方法中,您可以检测到
用户按下哪个键。下面的代码供您参考。希望有帮助。
You could set a key listener on the activity, and in the callback method, you could detect
which key the user hit. The code below is for your reference. Hope it helps.