pouria pouria - 5 months ago 44
Android Question

afterTextChange in TextWatcher how handle backspace and setText()?

I have an EditText and I use

afterTextChange()
to get values for every character, character by character. Well I have a button to do
editText.setText("")
but when I use that I get this error


charAt: 0 >= length 0.



How can I handle this error and how can I handle backspace?

My code is :

@Override
public void afterTextChanged(Editable s) {


enigmaCoding(String.valueOf(s.charAt(ii)));
ii++;

}

});


public void restart(View view) {

ii = 0;
txtV.setText("");
etxtC.setText("");

N J N J
Answer

You can check

if(s!=null && s.toString().length()>0)
{
  currentLength = s.toString().length();
  if(currentLength < previousLength){
      --ii;
  }else{
     enigmaCoding(String.valueOf(s.charAt(ii)));
     ii++;
 }
    previousLength = currentLength;// store previous length
}
Comments