开发者

How to send key event to an edit text

开发者 https://www.devze.com 2023-02-06 14:25 出处:网络
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.

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)
{
  // her开发者_运维百科e 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.
}


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.


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.


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


virsir , I suppose you are looking for dispatching hard keys programmatically.

For that you may try dispatch (KeyEvent.Callback receiver, KeyEvent.DispatcherState state, Object target) with an example at Back and other hard keys: three stories

Hope that helps.


Check for key events in your activity. for example, this code listens for back keypress:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
    finish();
    }
    return super.onKeyDown(keyCode, event);
}


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


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


Take a look at this article: creating-input-method.html. Basically, you can either manually send KeyEvents or you can manually edit and commit text around the cursor in the application's Input View.These are all done via your IME's InputConnection.

Hope this helps,


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


try implementing TextWatcher interface.

it has 3 methods which you need to override.

public void afterTextChanged(Editable s) {

    Log.v("afterTextChanged","here");
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    Log.v("beforeTextChanged","here");
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
}

I think this will work.

0

精彩评论

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