How to send key event to an edit text

前端 未结 10 2415
醉酒成梦
醉酒成梦 2021-02-07 20:43

For example, send a backspace key to the edit text control to remove a character or send a char code like 112 to append a character in the edittext control programmatically.

10条回答
  •  自闭症患者
    2021-02-07 21:32

    To send a simulated backspace key press to an EditText you have to send both key press and release events. Like this:

    mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
        KeyEvent.KEYCODE_DEL, 0));
    mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_UP,
        KeyEvent.KEYCODE_DEL, 0));
    

    This can be used to send any other key code, not just delete.

提交回复
热议问题