Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
814 views
in Technique[技术] by (71.8m points)

android - Send backspace key event to edit text

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));
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...