How can I instantly display text from an Edittext in a TextView?
I am basically trying to do this simple thing: I have got an Edittext and
a TextView. Now I would like to instantly (character by character) display
the text that is entered in the edittext in the textview. In addition,
when the backspace key is pressed and one charcter is deleted from the
editext, this should also happen in the textview.
In short: I want to display everything from the edittext immediately in
the textview. How can I do that?
Here is some code that I tried, but that didn't work for me. When I run it
on a emulator with Android 4.0 (and use the computer keyboard for input),
every character is displayed twice in the textview and there is no text in
the edittext. On the other hand, when I use an emulator with Android 2.3.3
(and use the emulator keyboard for input) there is no text in the
textview, however the edittext works fine.
In onCreate() I call my method setListener(). This looks like this:
private void setListener() {
mEdittext.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
String str = mTextview.getText().toString();
if(keyCode == KeyEvent.KEYCODE_DEL && str.length() > 0) {
str = str.substring(0, str.length()-1);
} else {
char newChar = (char)event.getUnicodeChar();
str = str.concat(Character.toString(newChar)) ;
}
mTextview.setText(str);
return true;
}
});
}
Any ideas to fix this?
No comments:
Post a Comment