如何将关键事件发送到编辑文本

如何将关键事件发送到编辑文本

本文介绍了如何将关键事件发送到编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,将退格键发送到编辑文本控件以删除字符或发送字符代码(如112)以编程方式在edittext控件中附加字符。

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.

实际上,我需要一个方法,如

Actually, I need a method like

void onKeyReceived(int keyCode)
{
  // here I would like to append the keyCode to EditText, I know how to add a visible character, but what about some special keys, like arrow key, backspace key.
}


推荐答案

发送模拟退格键按下EditText,您必须发送按键和释放事件。像这样:

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.

这篇关于如何将关键事件发送到编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:59