EditText OnKeyListener not working

前端 未结 5 1569
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 17:57

I know this/similar question has been asked before but the solution given is not working for me so I\'m asking again. I tried the solution given in that answer but still my

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 18:35

    The documentation states that the key events will only be propagated for the hardware key strokes, not software.

    http://developer.android.com/reference/android/view/View.OnKeyListener.html

    The device manufacturers are actually being discouraged to propagate soft keyboard events through key listeners, although it is completely up to the manufacturer to honour that or to actually treat the soft and hard keyboards with equal terms.

    Starting from Android 4.2.2, Android system itself will not support key stoke events for the soft keyboards at all, so even the manufacturers will not be able to choose their way.

    So the only foolproof option here is to implement your own IME (soft keyboard), and handle the keystrokes yourself.

    TextWatcher can be used mostly to replace the key listeners, however editText.setText(...); will also trigger the TextWatcher events, so if one is interested in typed keys only then probably TextWatcher is not a solution either.

    Please be cautious when using TextWatcher with AutocomleteTextView or EditText. Do not modify text in the AutocompleteTextView / EditText's content from within TextWatcher events, cause otherwise you'll most probably end up in an infinite event/listening loop.

提交回复
热议问题