问题
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?
回答1:
You can use the TextWatcher to set the TextView as text is entered:
TextWatcher watcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
mTextview.setText(mEdittext.getText().toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
mEdittext.addTextChangedListener(watcher);
回答2:
Try this:
@Override
protected void onStart() {
final EditText edit = (EditText) findViewById(R.id.editText1);
edit.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(edit.getText().toString());
return false;
}
});
super.onStart();
}
回答3:
If you want to print to textview after you move to next field, you can use onFocusChangeListner
//onCreate TextView tv = (TextView) findViewById(R.id.textView1); EditText et = (EditText) findViewById(R.id.editText1); et.setOnFocusChangeListener(mylistener); private OnFocusChangeListener mylistener = new OnFocusChangeListener(){ @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if(!hasFocus) { tv.setText(et.getText().toString()); } } };If you want to keep changing text dynamically as keys are entered use:
et.addTextChangedListener(watcher); TextWatcher watcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { if(s.length() > 0) tv.setText(s.toString()); }};
or
use OnKeyListener like eickeee explained or TextWatcher's onTextChanged event like TronicZomB explained
来源:https://stackoverflow.com/questions/18359811/how-can-i-instantly-display-text-from-an-edittext-in-a-textview