我不知道如何从我自己的按钮向 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模板网!
在android中剪切、复制、粘贴Cut, copy, paste in android(在android中剪切、复制、粘贴)
android EditText 融入背景android EditText blends into background(android EditText 融入背景)
更改 EditText 的线条颜色 - AndroidChange Line Color of EditText - Android(更改 EditText 的线条颜色 - Android)
EditText 始终显示带 2 位小数的数字EditText showing numbers with 2 decimals at all times(EditText 始终显示带 2 位小数的数字)
更改光标在展开的 EditText 中的开始位置Changing where cursor starts in an expanded EditText(更改光标在展开的 EditText 中的开始位置)
android中的EditText,adjustPan,ScrollView问题EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView问题)