Android call setText within onTextChanged

独自空忆成欢 提交于 2019-12-23 14:01:48

问题


To prevent the infinite loop I did something as ugly as this...

    @Override
protected void onTextChanged(CharSequence text, int start,
        int lengthBefore, int lengthAfter) {

    String t = text.toString();
    String tt = t.toUpperCase();

    if (!t.equals(tt)) {

        setText(tt);
    }

    super.onTextChanged(text, start, lengthBefore, lengthAfter);
}

Is there any other way to prevent the onTextChanged from being called when changing the text within onTextChanged?


回答1:


You could use flags that specify if the text has been changed once or not. If it has been changed once, then do not change again, else change it.

int flag_text=0;
protected void onTextChanged(CharSequence text, int start,
    int lengthBefore, int lengthAfter) {

    if (flag_text==0) {
        flag_text=1;
        setText(tt);
    }
    super.onTextChanged(text, start, lengthBefore, lengthAfter);
}


来源:https://stackoverflow.com/questions/9357543/android-call-settext-within-ontextchanged

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