Problems with intercepting a space key in EditText

I am trying to make a way for a user to enter text in a TextView from an EditText. However, if the user enters something and wants to fix it, I want them to be able to press the space bar on an empty EditText to get the last thing they wrote. The first problem is that if they type β€œhello”, press enter to add it to the TextView (which will clear it from EditText), and then press the space bar and EditText β€œhello”. Not what I want, and I cannot understand why.

My code to put the entered text on a hold line:

b1 = ti.getText().toString(); 

Then, if the user presses the space bar, I believe that they should get b1 in the EditText. Instead, I get: " " + b1 . Why is this added space?

 if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){ if(ti.getText().toString().equals("")){ ti.setText(b1); } 

My second, big problem is that the above code only works on a hardware keyboard. What is the key event for pressing the soft keyboard?

This is all onKeyListener.

+4
source share
2 answers

An easy way to solve the problem with extra space is to simply remove the space from the line before you put it in an EditText

 b1 = b1.substring(1); //<--- cut out the first character, which is the " " in your case. ti.setText(b1); 

ps I highly recommend more descriptive variable names. Your programs will probably be confusing if you use names like ti and b1. Perhaps this choice makes more sense in the context of your program. But from what you have shown here, it is not easy to say what these names refer to.

+1
source

For your first problem, I suspect you need to return true from your onKey method to onKeyListener when the "if" condition is true to indicate that the event was destroyed, otherwise you will get the default onKeyListener adding space to the EditText, that is: .

 if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){ if(ti.getText().toString().equals("")){ ti.setText(b1); return true; } } 

This excerpt from KeyEvent API docs should help with Problem 2: "Because I / O methods can use multiple and ingenious ways to enter text, there is no guarantee that any keystroke on a soft keyboard will trigger a key event: this is at the discretion of the IME, and actually sending such events is not recommended. You should never rely on getting KeyEvents for any key in the soft input method. In particular, the default software keyboard will never send any key If an event is targeted at an application targeting Jelly Bean or newer and will only send events for some clicks, delete and return the keys to applications using Ice Cream Sandwich or earlier. Please note that other software input methods can never send key events regardless of version: Consider using editor actions such as IME_ACTION_DONE if you need some interaction with the software keyboard, as it gives more visibility to the user, as your application voltage will respond to keystrokes. "

+1
source

All Articles