Detect changes in EditText (TextWatcher ineffective)

主宰稳场 提交于 2019-12-01 05:56:59

Use a textwatcher and do the diff yourself. store the previous text inside the watcher, and then compare the previous text to whatever sequence you get onTextChanged. Since onTextChanged is fired after every character, you know your previous text and the given text will differ by at most one letter, which should make it simple to figure out what letter was added or removed where. ie:

new TextWatcher(){ 
    String previousText = theEditText.getText();

    @Override 
    onTextChanged(CharSequence s, int start, int before, int count){
        compare(s, previousText); //compare and do whatever you need to do
        previousText = s;
    }

    ...
}

The best approach you can follow to identify text changes.

var previousText = ""


override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    previousText = s.toString()
}

override fun onTextChanged(newText: CharSequence?, start: Int, before: Int, count: Int) {

    val position = start + before ;

    if(newText!!.length > previousText.length){ //Character Added
        Log.i("Added Character", " ${newText[position]} ")
        Log.i("At Position", " $position ")
    } else {  //Character Removed
        Log.i("Removed Character", " ${previousText[position-1]} ")
        Log.i("From Position", " ${position-1} ")
    }
}

override fun afterTextChanged(finalText: Editable?) { }

You need to store and update the previous CharSequence every time the text is changed. You can do so by implementing the TextWatcher.

Example:

final CharSequence[] previousText = {""};
editText.addTextChangedListener(new TextWatcher()
{
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
    {
    }

    @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
    {
        if(i1 > 0)
        {
            System.out.println("Removed Chars Positions & Text:");
            for(int index = 0; index < i1; index++)
            {
                System.out.print((i + index) + " : " + previousText[0].charAt(i + index)+", ");
            }
        }
        if(i2 > 0)
        {
            System.out.println("Inserted Chars Positions & Text:");
            for(int index = 0; index < i2; index++)
            {
                System.out.print((index + i) + " : " + charSequence.charAt(i + index)+", ");
            }
            System.out.print("\n");
        }
        previousText[0] = charSequence.toString();//update reference
    }

    @Override public void afterTextChanged(Editable editable)
    {
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!