在修改交易密碼界面用到一個自定義輸入框
類似這種(原諒丑了點~)
結果昨天有用戶反饋在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
以上所述是小編給大家介紹的VIVO手機上del鍵無效OnKeyListener不響應的原因及解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!