Android虚拟键盘事件
我正在尝试使用 4 个 EditText 块来实现类似 iPhone 的 PIN 码授权,如下所示:
我正在使用TextWatcher 检查字段是否已更改,以便我可以在块之间跳转。 这是代码:
@Override
public void afterTextChanged(Editable s) {
if (del && index > 0) {
pin[index - 1].requestFocus();
}
else if ( ! del && ind < 3) {
pin[index + 1].requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
del = count != 0;
}
一切正常,除了当块为空并且按下 DEL/BACKSPACE 时,我想返回到上一个块并清除其值。 这就是 TextWatcher 失败的地方,因为空块中没有进行任何更改,所以它没有执行任何操作。
我尝试过使用 keyEventListener,但它仅捕获模拟器上的事件,而不是通过虚拟键盘捕获实际设备上的事件。
任何人都可以建议我如何捕捉 DEL 事件或任何其他方式来实现这个想法吗?
I'm trying to implement iPhone like PIN-code authorization with 4 EditText blocks like this:
I'm using TextWatcher to check if field was changed so I can jump between blocks.
Here is the code:
@Override
public void afterTextChanged(Editable s) {
if (del && index > 0) {
pin[index - 1].requestFocus();
}
else if ( ! del && ind < 3) {
pin[index + 1].requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
del = count != 0;
}
Everything works fine except when block is empty and DEL/BACKSPACE is pressed, I want to go back to previous block and clear its' value.
This is where TextWatcher fails me since no change was made in empty block it doesn't do anything.
I have tried using keyEventListener but it only catches events on emulator not on actual device via virtual keyboard.
Can anyone suggest an idea of how can I catch DEL event or any other way to implement this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以检查更改前后该字段是否为空?不知道任何其他按键是否会使该字段留空,因此您可以说,按下了后退键并跳转到上一个字段。好吧,这不是真正的技术解决方案,而只是看待问题的不同方式。
Maybe you can check if the field is empty before and after change? Don't know if any other key press can leave the field empty and so you can say, back was pressed and jump to previous field. Ok this is not a real technical solution, but just a different way of viewing the problem.
我找到了解决此问题的方法。这可能不是最好的解决方案,但它对我有用。
除了
TextWatcher
之外,我还向块添加了InputFilter
我还认为将其余代码从
TextWatcher
移植到是一个更好的主意>输入过滤器
I found a workaround for this issue. This is probably not the best solution, but it works for me.
Aside from
TextWatcher
I've addedInputFilter
to blocksI also think it's a better idea to port the rest of the code from
TextWatcher
toInputFilter