0

My phone was just updated from Android 6.0 to 7.0. After the update I notice a feature in my app is not working correctly, that is, the EditText will not accept the entered character and will instead repeat the previously entered character. The control keyboard is set to CapCharacter, so caps are there. If I set it to caps lock, it works correctly.

Following are the relevant code segments

    <EditText
    android:id="@+id/etEntry"
    style="@android:style/Widget.EditText"
    android:digits="cvABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*?.,^+[](|){}\\~-"
    android:hint="@string/searchterm"
    android:imeOptions="actionSearch|flagForceAscii"
    android:inputType="textCapCharacters|textNoSuggestions"
    android:singleLine="true"
    android:visibility="visible"
    tools:hint="Search Term" />

There is a TextWatcher for the control, and the stype value is 3 (pattern) so it bypasses the section that modifies the input.

        private boolean mWasEdited = false;
    @Override
    public void afterTextChanged(Editable s) {
        if (mWasEdited){
            mWasEdited = false;
            return;
        }
        mWasEdited = true;
        String enteredValue  = s.toString();
        if (stype.getSelectedItemPosition() != 3) { // not pattern
            String newValue = enteredValue.replaceAll("[cv0123456789.,^+-]", "");
            int caret = etTerm.getSelectionStart();
            if (stype.getSelectedItemPosition() != 0 && // not anagram
                    stype.getSelectedItemPosition() != 3) { // and not pattern
                newValue = newValue.replaceAll("[*]", "");
            }
            if (Arrays.asList(2,7,8).contains(stype.getSelectedItemPosition())) { // hooks, begins, ends
                newValue = newValue.replaceAll("[?]", "");
            }
            etTerm.setText(newValue);
            etTerm.setSelection(Math.min(newValue.length(), caret)); // if first char is invalid
        }
    }
};

I assume I either have to configure the keyboard aspects of the control, on do something is onTextChanged. It's a mystery.

Dana Bell
  • 69
  • 6

1 Answers1

0

I figured it out, though I don't understand why it's required since I didn't change anything in the EditText.

I changed the last few lines of code, declaring caret before the conditional, adding a return to the inner conditional, and adding two lines afterward for stype 3.

    public void afterTextChanged(Editable s) {
        if (mWasEdited){
            mWasEdited = false;
            return;
        }
        mWasEdited = true;
        String enteredValue  = s.toString();
        int caret = etTerm.getSelectionStart();
        if (stype.getSelectedItemPosition() != 3) { // not pattern
            String newValue = enteredValue.replaceAll("[cv0123456789.,^+-]", "");
            if (stype.getSelectedItemPosition() != 0 && // not anagram
                    stype.getSelectedItemPosition() != 3) { // and not pattern
                newValue = newValue.replaceAll("[*]", "");
            }
            if (Arrays.asList(2,7,8).contains(stype.getSelectedItemPosition())) {
                newValue = newValue.replaceAll("[?]", "");
            }
            etTerm.setText(newValue);
            etTerm.setSelection(Math.min(newValue.length(), caret)); 
            return;
        }
        etTerm.setText(enteredValue);
        etTerm.setSelection(Math.min(enteredValue.length(), caret)); 
    }
};
Dana Bell
  • 69
  • 6