Skip to content Skip to sidebar Skip to footer

Edittext Not Updated After Text Changed In The Textwatcher

I have an EditText and a TextWatcher. Skeleton of my code: EditText x; x.addTextChangedListener(new XyzTextWatcher()); XyzTextWatcher implements TextWatcher() { public synchro

Solution 1:

Ok, you never actually change the EditText just the Editable. Android EditTexts are not children of the Editable class. Strings are subclasses of the Editable class. The onTextChangedListener doesn't receive the EditText as an argument but the Editable/String displayed in the EditText. After you format the Editable with the hyphens you then need to update the EditText. Something like this should work fine:

classMyClassextendsActivity{

    //I've ommited the onStart(), onPause(), onStop() etc.. methods

    EditText x;
    x.addTextChangedListener(newXyzTextWatcher());

    XyzTextWatcher implementsTextWatcher() {
        publicsynchronizedvoidafterTextChanged(Editable text) {
            Strings= formatText(text);
            MyClass.this.x.setText(s);
        }
    }

}

To prevent the slowdown why not change the formatText method something like this?

private Editable formatText(Editable text){
    int sep1Loc = 3;
    int sep2Loc = 7;

    if(text.length==sep1Loc)
    text.append('-');

    if(text.length==sep2Loc)
    text.append('-');

    return text;
}

Note: I haven't tested this

Post a Comment for "Edittext Not Updated After Text Changed In The Textwatcher"