How to delete text in EditText using TextWatcher? Each time it detects a string and formats it

I have an EditText with a TextWatcher. It is supposed to format the text to the height of the person, for example, 5'9 ". But when the user makes a mistake and wants to delete the foggy character, TextWather does not allow him to do this. Let them say that the user wants to delete" characther the TextWather immediately returns it back. Below is the code. So, how do I allow a user to delete text in an 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()); } 

}

+6
source share
2 answers

Ignoring the fact that Karakuri reported an incorrect callback to your code, you can add a simple fix in which you just listen to which key the user uses.

Without any real tests or further improvements for your existing code, this seems to fix the problem described:

 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()); } } } 
+8
source

From the docs :

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

This method is called to notify you that, within the s counter, characters starting at the beginning have just replaced the old text, which is the length before it. An error occurred while trying to make changes to s from this callback.

You should make any changes to afterTextChanged (), and not to any of the other two callbacks.

public abstract void afterTextChanged (editable)

This method is called to notify you that somewhere in s the text has changed. It is permissible to make further changes to s from this callback, but be careful not to end up in an infinite loop, because any changes you make will call this method again recursively. (You are not told where the change occurred because the other afterTextChanged() methods may have already made other changes and will invalidate the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place, and then look from here where the span ended.)

+2
source

All Articles