5

I'm using a AutoCompleteTextView, the default behavior of the backspace button goes something like this.

Say i type "Ar", this gives me a suggestion "Argentina", i select "Argentina" from the drop down...The Text now becomes "Argentina ". But say i need to remove the last character, so I hit backspace on the keyboard, the AutcompleteTextView removes all the text till the point i typed (ie. the text now becomes "Ar" again).

How do i eliminate this behavior and let the text in the AutoComplete to behave normally?

At first I thought it was some kind of SpannableString so i called "clearSpans()" but it doesn't seem to work. Any pointers?

Thanks in advance. :)

st0le
  • 33,375
  • 8
  • 89
  • 89

2 Answers2

1

I think you use the MultiAutoCompleteTextView which add the setTokenizer(new SpaceTokenizer()). If you use AutoCompleteTextView instead of MultiAutoCompleteTextView and remove the setTokenizer(...) the problem will be gone.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
Ted Yu
  • 304
  • 3
  • 13
0

I did not find any solution, but finally I figured out, this code worked for me:

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            try {
                // removing QwertyKeyListener.Replaced span
                Editable text = editText.getText();
                Object[] spans = text.getSpans(0, text.length(), Object.class);
                if (spans != null) {
                    for (int i = spans.length - 1; i >= 0; i--) {
                        Object o = spans[i];
                        String desc = "" + o; // This is a hack, not a perfect solution, but works. "QwertyKeyListener.Replaced" is a private type
                        if (desc.indexOf("QwertyKeyListener$Replaced") != -1) {
                            text.removeSpan(o);
                        }
                    }
                }
            } catch (Throwable e) {
                MyUtil.msgError(e);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
Miki
  • 440
  • 4
  • 7