开发者

Send backspace key event to edit text

开发者 https://www.devze.com 2023-03-29 23:29 出处:网络
I don\'t know how to send a backspace key event to a EditText from my own button. Here is what i tried:

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=(Bu开发者_运维知识库tton)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));
    }
});


From the Android developer docs:

public KeyEvent (int action, int code)

Create a new key event.

Parameters
action Action code: either ACTION_DOWN, ACTION_UP, or ACTION_MULTIPLE.
code The key code

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

getAction() value: the key has been pressed down.

So this should work:

@Override
public void onClick(View v)
{
    text.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消