Cannot clear edittext after setText()
this is my code , but the problem is when the I want to clear some chars i cannot
TWL=new TextWatcher(){
public void开发者_C百科 afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
name.removeTextChangedListener(this);//after this line you do the editing code
name.setText(s+"-");
name.setSelection(name.getText().length());
name.addTextChangedListener(TWL); // you register again for listener callbacks
}};
name = (EditText)findViewById(R.id.editText1);
name.addTextChangedListener(TWL);
I've experienced a quite similar problem. I had a EditText with a OnKeyListener, but it did not let me delete any space in the edittext (I didn't listen to any "delete button" event). I figured out what's wrong when I tried to return "false" in my listener (event not completed). Not it works like a charm. Are you sure you don't have any key listener on that? In that case, do you return false when you "grab" the event?
Hope it helps
((EditText)findViewById(R.id.main_nome)).setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
//hide the keyboard
InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(findViewById(R.id.main_nome).getWindowToken(), 0);
iniziaClicked(view);
findViewById(R.id.loseFocus).requestFocus();
}
return false;
}
});
精彩评论