当前位置:主页 > 移动开发 > iOS代码 >

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

时间:2020-11-08 12:16:07 | 栏目:iOS代码 | 点击:

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

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

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

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

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

代码如下:

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://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event%22

您可能感兴趣的文章:

相关文章