我正在尝试通过发送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));
字符添加到编辑文本中。