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.
}
I think you need use
addTextChangedListener
toEditText
.Refer the answer of EditText input with pattern android and Live editing of users input
to simulate backspace key, just ad code
to simulate adding a character, put the code
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
just use the setText method to do this. If you are wanting to simulate a backspace you could do something like this.
To send a simulated backspace key press to an EditText you have to send both key press and release events. Like this:
This can be used to send any other key code, not just delete.
if you want a click listener, the best way to do it is this:
if you want a backspace button, do this:
if you want to append a character in the EditText, do this:
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.Whether to return
true
for each case you have handled (as above) or to always returnsuper.onKeyDown(keyCode, event);
after yourswitch
statement will depend on your exact requirements. Check the documentation for the behaviour ofonKeyDown
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 theTextView
documentation for the different methods you can call. Also look at theKeyEvent
documentation for a list of the keys you can check for.