如何将按键事件发送到编辑文本
例如,向编辑文本控件发送退格键以删除字符,或发送字符代码(如 112)以编程方式在编辑文本控件中附加字符。
实际上,我需要一个像这样的方法
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.
}
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.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
要将模拟的退格按键发送到 EditText,您必须发送按键和释放事件。像这样:
这可以用于发送任何其他键代码,而不仅仅是删除。
To send a simulated backspace key press to an EditText you have to send both key press and release events. Like this:
This can be used to send any other key code, not just delete.
您的问题不太清楚,但我认为您想在按下某些按钮时修改/附加文本到
TextView
。如果是这样,您需要一些现有答案的组合。是否为您处理的每种情况返回
true
(如上所述),还是在switch
super.onKeyDown(keyCode, event); > 声明将取决于您的具体要求。检查文档以了解onKeyDown
如果您想要删除字符或移动光标,而不是在每种情况下附加文本,则可以在每个
case
语句中执行此操作。查看TextView
文档您可以调用的不同方法。另请参阅KeyEvent
文档您可以检查的键列表。Your question is not all that clear, but I think you want to modify/append text to a
TextView
when certain buttons are pressed. If so, you want a combination of some of the existing answers.Whether to return
true
for each case you have handled (as above) or to always returnsuper.onKeyDown(keyCode, event);
after yourswitch
statement will depend on your exact requirements. Check the documentation for the behaviour ofonKeyDown
If, instead of appending text in each case you want to delete a character, or move the cursor, you could do that in each
case
statement. Have a look at theTextView
documentation for the different methods you can call. Also look at theKeyEvent
documentation for a list of the keys you can check for.我认为您需要使用
addTextChangedListener
到EditText
。请参考 EditText input with pattern android 和 实时编辑用户输入
I think you need use
addTextChangedListener
toEditText
.Refer the answer of EditText input with pattern android and Live editing of users input
virsir,我想您正在寻找以编程方式调度硬键的方法。
为此,您可以尝试 调度(KeyEvent.Callback 接收器、KeyEvent.DispatcherState 状态、对象目标),示例位于 返回键和其他硬键:三个故事
希望有所帮助。
virsir , I suppose you are looking for dispatching hard keys programmatically.
For that you may try dispatch (KeyEvent.Callback receiver, KeyEvent.DispatcherState state, Object target) with an example at Back and other hard keys: three stories
Hope that helps.
检查您活动中的关键事件。例如,此代码侦听
back
按键:Check for key events in your activity. for example, this code listens for
back
keypress:只需使用 setText 方法即可完成此操作。如果你想模拟退格键,你可以这样做。
just use the setText method to do this. If you are wanting to simulate a backspace you could do something like this.
要模拟退格键,只需添加代码
来模拟添加字符,放入代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
to simulate backspace key, just ad code
to simulate adding a character, put the code
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
看看这篇文章:creating-input-method.html。基本上,您可以手动发送
KeyEvents
,也可以在应用程序的输入视图
中手动编辑和提交光标周围的文本
。这些都已完成通过 IME 的InputConnection
。希望这有帮助,
Take a look at this article: creating-input-method.html. Basically, you can either manually send
KeyEvents
or you can manuallyedit and commit text around the cursor
in the application'sInput View
.These are all done via your IME'sInputConnection
.Hope this helps,
如果您想要一个点击侦听器,最好的方法是:
如果您想要一个退格按钮,请执行以下操作:
如果您想在 EditText 中附加一个字符,请执行以下操作:
if you want a click listener, the best way to do it is this:
if you want a backspace button, do this:
if you want to append a character in the EditText, do this:
尝试实现 TextWatcher 接口。
它有 3 个你需要重写的方法。
我认为这会起作用。
try implementing TextWatcher interface.
it has 3 methods which you need to override.
I think this will work.