Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing a multiline EditText

I am trying to clear a multiline EditText field inside the OnEditorActionListener.onEditorAction method.

But using any of the obvious ways i.e.

((EditText) view).getEditableText().clear();
((EditText) view).getEditableText().clearSpans(); 
((EditText) view).setText("");

only clears the visible characters - leaving the the newlines in the field (which then have to be manually deleted).

Is there way to 'completely' clear a multiline EditText field ? (or at least - does anybody know why the above don't work ?)

like image 621
tonys Avatar asked Jan 25 '11 14:01

tonys


1 Answers

Solved (in a minute after a good night's sleep) - the newline was being added after clearing the text because the onEditorAction method implementation was returning false (for other reasons).

Returning true indicates that the 'enter' has been processed/consumed and the clear() behaves as expected:

edittext.setOnEditorActionListener(new OnEditorActionListener() { 
    @Override
    public boolean onEditorAction(TextView view,int actionId,KeyEvent event) {
           post(view.getText().toString());

           ((EditText) view).getEditableText().clear();

           return true;
         }
     });
like image 116
tonys Avatar answered Oct 26 '22 11:10

tonys