How to send key event to an edit text

2020-02-26 06:35发布

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.
}

10条回答
该账号已被封号
2楼-- · 2020-02-26 07:07

I think you need use addTextChangedListener to EditText.
Refer the answer of EditText input with pattern android and Live editing of users input

查看更多
女痞
3楼-- · 2020-02-26 07:09

to simulate backspace key, just ad code

editText.setText(editText.getText().substring(0,editText.getText().length()-1))
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

to simulate adding a character, put the code

editText.setText(editText.getText() + (char) charCode)

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

查看更多
在下西门庆
4楼-- · 2020-02-26 07:10

just use the setText method to do this. If you are wanting to simulate a backspace you could do something like this.

String curText = mEditText.getText();
if(!curText.equals("")){
    mEditText.setText(curText.subString(0, curText.length - 1));
}
查看更多
▲ chillily
5楼-- · 2020-02-26 07:11

To send a simulated backspace key press to an EditText you have to send both key press and release events. Like this:

mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
    KeyEvent.KEYCODE_DEL, 0));
mEditText.dispatchKeyEvent(new KeyEvent(0, 0, KeyEvent.ACTION_UP,
    KeyEvent.KEYCODE_DEL, 0));

This can be used to send any other key code, not just delete.

查看更多
我命由我不由天
6楼-- · 2020-02-26 07:11

if you want a click listener, the best way to do it is this:

View textfield = findViewById(R.id.textfield);  
textfield .setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) {  
/*your code for the click event here*/ }});

if you want a backspace button, do this:

public void backSpace() {   
EditText textfield = (EditText)findViewById(R.id.textfield);  
    try {  
        textfield.getText().delete(textfield.getSelectionEnd() - 1, textfield.getSelectionStart());  
    } catch (Exception e) {  
        try {  
            textfield.getText().delete(textfield.length() - 1, textfield.length());  
        } catch (Exception myException) {  
        //textfield.getText().delete(textfield.length(), textfield.length() - 1);  
        }  
    }  
}

if you want to append a character in the EditText, do this:

EditText textfield = (EditText)findViewById(R.id.textfield);  
textfield.setText(textfield.getText().concat("112"));
查看更多
倾城 Initia
7楼-- · 2020-02-26 07:12

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.

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    (TextView) textView = (TextView) findViewById(R.id.myTextView);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    switch(keyCode) {
    case KeyEvent.KEYCODE_BACK:
        // user pressed the "BACK" key.  Append "_back" to the text
        textView.append("_back");
        return true;
    case KeyEvent.KEYCODE_DEL:
        // user pressed the "BACKSPACE" key.  Append "_del" to the text
        textView.append("_del");
        return true;
    default:
        return super.onKeyDown(keyCode, event);
    }
}

Whether to return true for each case you have handled (as above) or to always return super.onKeyDown(keyCode, event); after your switch statement will depend on your exact requirements. Check the documentation for the behaviour of onKeyDown

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 the TextView documentation for the different methods you can call. Also look at the KeyEvent documentation for a list of the keys you can check for.

查看更多
登录 后发表回答