VIVO手机上del键无效OnKeyListener不响应的原因及解决方法

时间:2022-09-19 20:21:06

在修改交易密码界面用到一个自定义输入框

类似这种(原谅丑了点~)

结果昨天有用户反馈在VIVO手机上回出现,Del键无效的问题~~~

最后发现是:EdiText的OnKeyListener没有响应。

最后搜搜了一下,找到了解决方案:重写EdiTextView的InputConnectionWrapper的方法后解决了问题:

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class ZanyEditText extends EditText {
  private OnDelKeyEventListener delKeyEventListener;
  public ZanyEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }
  public ZanyEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public ZanyEditText(Context context) {
    super(context);
  }
  @Override
  public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
        true);
  }
  private class ZanyInputConnection extends InputConnectionWrapper {
    public ZanyInputConnection(InputConnection target, boolean mutable) {
      super(target, mutable);
    }
    @Override
    public boolean sendKeyEvent(KeyEvent event) {
      if (event.getAction() == KeyEvent.ACTION_DOWN
          && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
        if (delKeyEventListener != null) {
          delKeyEventListener.onDeleteClick();
          return true;
        }
      }
      return super.sendKeyEvent(event);
    }
    @Override
    public boolean deleteSurroundingText(int beforeLength, int afterLength) {
      if (beforeLength == 1 && afterLength == 0) {
        return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
            KeyEvent.KEYCODE_DEL))
            && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
            KeyEvent.KEYCODE_DEL));
      }
      return super.deleteSurroundingText(beforeLength, afterLength);
    }
  }
  /**
   *
   * 功能描述: <br>
   * 〈功能详细描述〉
   *
   * @param delKeyEventListener EditText删除回调
   */
  public void setDelKeyEventListener(OnDelKeyEventListener delKeyEventListener) {
    this.delKeyEventListener = delKeyEventListener;
  }
  public interface OnDelKeyEventListener {
    void onDeleteClick();
  }
}

参考:http://*.com/questions/4886858/android-edittext-deletebackspace-key-event%22

以上所述是小编给大家介绍的VIVO手机上del键无效OnKeyListener不响应的原因及解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/yanghuiyu38/article/details/53638601