简体   繁体   English

Android虚拟键盘事件

[英]Android virtual keyboard event

I'm trying to implement iPhone like PIN-code authorization with 4 EditText blocks like this: 我正在尝试使用4个EditText块来实现类似PIN码授权的iPhone:

iPhone PIN码授权

I'm using TextWatcher to check if field was changed so I can jump between blocks. 我正在使用TextWatcher检查字段是否已更改,以便可以在块之间跳转。 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. 一切正常,除了当块为空并且按了DEL / BACKSPACE时,我想返回上一个块并清除其值。 This is where TextWatcher fails me since no change was made in empty block it doesn't do anything. 这是TextWatcher使我失败的地方,因为在空块中没有进行任何更改,因此它什么也没做。

I have tried using keyEventListener but it only catches events on emulator not on actual device via virtual keyboard. 我尝试使用keyEventListener,但是它只能通过虚拟键盘捕获模拟器上的事件,而不是实际设备上的事件。

Can anyone suggest an idea of how can I catch DEL event or any other way to implement this? 任何人都可以提出关于如何捕获DEL事件或其他任何实现方法的想法吗?

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. 好的,这不是真正的技术解决方案,而只是解决问题的另一种方法。

I found a workaround for this issue. 我找到了解决此问题的方法。 This is probably not the best solution, but it works for me. 这可能不是最佳解决方案,但对我有用。 Aside from TextWatcher I've added InputFilter to blocks 除了TextWatcher我还添加了InputFilter到块

@Override
public CharSequence filter(CharSequence source, int start, int end,
        Spanned dest, int dstart, int dend) {
    if ( end == 0 && ind > 0 && dest.length() == 0 ) {
        pin[ind - 1].requestFocus();
    }
    return null;
}

I also think it's a better idea to port the rest of the code from TextWatcher to InputFilter 我也认为将其余代码从TextWatcherInputFilter是一个更好的主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM