通过发送Ctrl + Z调用EditText的内置撤消功能

时间:2019-02-27 10:26:02

标签: android android-edittext keyevent undo

我正在尝试通过发送wrap_content来执行Android EditText的内置撤消功能(通过TextView):

Ctrl+Z

但是,它不起作用(什么也不做)。如果我连接了蓝牙键盘并输入public static KeyEvent keyEvent(int keycode, int metaState) { final long currentTime = System.currentTimeMillis(); return new KeyEvent(currentTime, currentTime, KeyEvent.ACTION_DOWN, keycode, 0, metaState); } mEditText.dispatchKeyEvent(keyEvent(KeyEvent.KEYCODE_Z, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON)); ,它将起作用,并且在“编辑文本”中执行撤消操作。

也可以只发送字母Ctrl+Z,不发送z,并在编辑文本中添加Ctrl字符:

z

我也尝试过(基于this answer和评论):

mEditText.dispatchKeyEvent(keyEvent(KeyEvent.KEYCODE_Z, 0));

但是它只是将mEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_CTRL_LEFT)); mEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_X)); mEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_X)); mEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_CTRL_LEFT)); 字符添加到编辑文本中。

0 个答案:

没有答案