我不知道如何从我自己的按钮向 EditText
发送退格键事件.这是我尝试过的:
I don't know how to send a backspace key event to a EditText
from my own button. Here is what i tried:
Button backSpace=(Button)findViewById(R.id.backSpace_tab);
backSpace.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.dispatchKeyEvent(new KeyEvent(KeyEvent.KEYCODE_DEL,KeyEvent.KEYCODE_P));
}
});
来自Android 开发者文档:
public KeyEvent (int action, int code)
public KeyEvent (int action, int code)
创建一个新的关键事件.
Create a new key event.
参数
action 操作代码:ACTION_DOWN、ACTION_UP 或 ACTION_MULTIPLE.
代码 关键代码
Parameters
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code
第一个参数应该是一个动作代码.在您的情况下,您应该使用 ACTION_DOWN,因为您想模拟一个按键:
The first parameter should be an action code. In your case you should use ACTION_DOWN, because you want to simulate a keypress:
public static final int ACTION_DOWN
public static final int ACTION_DOWN
getAction() 值:按键已被按下.
getAction() value: the key has been pressed down.
所以这应该有效:
@Override
public void onClick(View v)
{
text.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
}
这篇关于发送退格键事件以编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!