I want to clear an EditText when i click a button or press Enter. It works fine with the button, but when pressing Enter, it clears both the EditText and TextView.
I appreciate any help. Thank you.
public void onClick(View v) {
textViewEcho.setText(editTextInput.getText().toString());
editTextInput.setText("");
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == 66) {
textViewEcho.setText(editTextInput.getText().toString());
editTextInput.setText("");
}
return false;
}
Update: i found out that onKey is called twice, that's why the TextView's content is cleared, but can't figure out why onKey is called twice, is this a bug in Android?!!!!!
i tried onKeyUp instead, it does work.
edittext.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER)
//write the code what ever u want when u press enter {
edittext.setText("");
}
return false;
}
});
try this, need to return true.
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == 66) {
String txt = editTextInput.getText().toString();
textViewEcho.setText(txt);
editTextInput.setText("");
return true; // return the true from here
}
return false;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.