How do I delete text in EditText with TextWatcher? Every time it detects a string and formats its

Sean Kilb picture Sean Kilb · Dec 28, 2012 · Viewed 11.9k times · Source

I have an EditText with a TextWatcher. It is supposed to format the text to a human height like 5'9". But when user makes a mistake and wants to delete the mistyped character the TextWather doesn't allow him to do that. Let's say user wants to delete " characther the TextWather adds its back right away. Below is the code. So how do I allow user to delete text in the EditText?

private class CustomTextWatcher implements TextWatcher {
    private EditText mEditText;

public CustomTextWatcher(EditText e) {
    mEditText = e;
}

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

public void onTextChanged(CharSequence s, int start, int before,
        int count) {


}

public void afterTextChanged(Editable s) {
    int count = s.length();
    String str = s.toString();
    if (count == 1) {
        str = str + "'";
    } else if (count == 3) {
        str = str + "\"";
    } else if ((count > 4) && (str.charAt(str.length() - 1) != '\"')) {
        str = str.substring(0, str.length() - 2)
                + str.charAt(str.length() - 1) + "\"";
    } else {
        return;
    }
    mEditText.setText(str);
    mEditText.setSelection(mEditText.getText().length());


}

}

Answer

Stefan de Bruijn picture Stefan de Bruijn · Dec 28, 2012

Ignoring the fact Karakuri posted about your code being in the wrong callback, you could add a simple fix where you just listen to what key the user uses.

Without any real testing or further improvements to your existing code, this does seem to fix your described problem:

package com.example.testwatchertest;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;

public class MainActivity extends Activity implements TextWatcher {

    EditText editText;
    boolean keyDel = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);
        editText.addTextChangedListener(this);

        editText.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_DEL){
                    keyDel = true;
                }else{
                    keyDel = false;
                }
                return false;
            }
        });

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (!keyDel) {

            String str = s.toString();
            if (count == 1) {
                str = str + "'";
            } else if (count == 3) {
                str = str + "\"";
            } else if ((count > 4) && (str.charAt(str.length() - 1) != '\"')) {
                str = str.substring(0, str.length() - 2) + str.charAt(str.length() - 1) + "\"";
            } else {
                return;
            }
            editText.setText(str);
            editText.setSelection(editText.getText().length());

        }

    }

}