views:

36

answers:

1

Hi,

I have a TextWatcher set on an EditText that changes the input type after a user types a number followed by a space.

If the user types two numbers the input type switches and accepts the next character, but if the user types only one number and presses space the input type still changes but it won't accept the first character the user tries to input.

I've tested this on Froyo and 1.6, it only happens on Froyo, 1.6 works like it should.

Here's the code:

    TextWatcher watcher = new TextWatcher() {
    @Override
    public void afterTextChanged (Editable s) {
    }

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

    @Override
    public void onTextChanged (CharSequence s, int start, int before, int count) {          
        // Parsed text holder is a class that just parses the EditText and pulls out various parts.
        ParsedTextHolder th = parseTextHolder(s);

        String newText = "";
        Boolean setTextKeyListener = false;

        String tGetTextString = mQuery.getText().toString();

        if (!th.pFullMatch.equals("")) {
            if (th.pFullMatch.length() == 2) {
                mQuery.setKeyListener(new
                TextKeyListener(TextKeyListener.Capitalize.SENTENCES, true));
                newText = tGetTextString + " for ";
                setTextKeyListener = true;
            }
        }

        if (setTextKeyListener) {
            Log.i("setTextKeyListener", "true");
            if (mQuery.getKeyListener().getClass() != TextKeyListener.class) {
                    mQuery.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.SENTENCES, true));
            } else {
                    Log.d("setTextKeyListener", "skipped. already was text.");
            }

            if (!newText.equals("")) {
                    int position = newText.length();
                    String ttext = newText;
                    newText = "";
                    mQuery.setText(ttext, TextView.BufferType.EDITABLE);
                    mQuery.setText(ttext);
                    Editable text = mQuery.getEditableText();
                    Log.w("setting selectiont to text: ", text.toString());
                    Log.w("setting selectiont to position: ", Integer.toString(position));
                    Selection.setSelection(text, position);
                    mQuery.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.SENTENCES, true));
            }

        }
    }
};

Also, here's an APK if you want to see what the bug is like: http://endlesswhileloop.com/files/KeyboardBug.apk

A: 

Is mQuery the editText that is being watched? According to the javadocs, you shouldn't be making any changes to the text in your EditText in onTextChanged. All such changes should be made in afterTextChanged.

Generally, I've ended up examining the change in onTextChanged and then doing the work that results form the change in afterTextChanged. You might try that.

Mayra
Just tried using afterTextChanged, no luck. Thanks for the answer though, I didn't realize that it was bad to edit the EditText in onTextChanged.
tonyc
Hmm, ok, why are you calling refreshDrawableState at the end? That seems a bit hackish, might be causing differences in different versions.. (This is just a guess, but often when you have to force the view to refresh you are really just masking an underlying problem).
Mayra
Ah, I had refreshDrawableState just as a hack to see if it would fix it, I removed a bunch of irrelevant stuff, still no luck though.
tonyc