Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't insert into Editable

I must be doing something obvious, but I can't figure out what it is. I'm simply trying to insert a character into an Editable:

@Override
public void afterTextChanged(Editable s) {
    Log.d(TAG, "inserting space at " + location);
    s.insert(location, " ");
    Log.d(TAG, "new word: '" + s + "'");
}

But s never changes. The string 's' is long enough, because I print it and it looks good. If I call Editable.clear(), it is cleared, and I can replace multiple characters with Editable.replace(). Ideas?

like image 586
Shawn Lauzon Avatar asked Feb 07 '11 18:02

Shawn Lauzon


2 Answers

I found the problem; I set the inputType as "number" and so adding the space silently failed.

like image 141
Shawn Lauzon Avatar answered Oct 22 '22 19:10

Shawn Lauzon


To edit an editable with input filters, simply save the current filters, clear them, edit your text, and then restore the filters.

Here is some sample code that worked for me:

@Override
public void afterTextChanged(Editable s) {
    InputFilter[] filters = s.getFilters(); // save filters
    s.setFilters(new InputFilter[] {});     // clear filters
    s.insert(location, " ");                // edit text
    s.setFilters(filters);                  // restore filters
}
like image 44
BeccaP Avatar answered Oct 22 '22 19:10

BeccaP