在修改交易密码界面用到一个自定义输入框
类似这种(原谅丑了点~)
结果昨天有用户反馈在VIVO手机上回出现,Del键无效的问题~~~
最后发现是:EdiText的OnKeyListener没有响应。
代码如下:
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); } } /** * * 功能描述:参考: http://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event%22
* 〈功能详细描述〉 * * @param delKeyEventListener EditText删除回调 */ public void setDelKeyEventListener(OnDelKeyEventListener delKeyEventListener) { this.delKeyEventListener = delKeyEventListener; } public interface OnDelKeyEventListener { void onDeleteClick(); } }