Custom EditText freezes on clearing text using backspace

为君一笑 提交于 2020-02-08 06:47:33

问题


I have implemented a Custom EditText which can take Bold, Italics, Underline text. Everything works fine except when I try to delete text by long pressing the backspace button. On long pressing backspace there is a delay in clearing the text.

Here is the overriden onTextChanged() method

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

    Log.d(VIEW_LOG_TAG,"Start: "+start+" Length before: "+lengthBefore+" Length After: "+lengthAfter+" TextLength: "+text.length());
    Spannable str = this.getText();
    CharacterStyle ss;
    UnderlineSpan ss1=null;
    int endLength = text.toString().length();

    switch (currentTypeface) {
        case TYPEFACE_NORMAL:
            ss = new StyleSpan(Typeface.NORMAL);
            break;
        case TYPEFACE_BOLD:
            ss = new StyleSpan(Typeface.BOLD);
            break;
        case TYPEFACE_ITALICS:
            ss = new StyleSpan(Typeface.ITALIC);
            break;
        case TYPEFACE_BOLD_ITALICS:
            ss = new StyleSpan(Typeface.BOLD_ITALIC);
            break;
        case TYPEFACE_UNDERLINE:
            ss= new UnderlineSpan();
            break;
        case TYPEFACE_BOLD_UNDERLINE:
            ss = new StyleSpan(Typeface.BOLD);
            ss1=new UnderlineSpan();
            break;
        case TYPEFACE_ITALICS_UNDERLINE:
            ss = new StyleSpan(Typeface.ITALIC);
            ss1=new UnderlineSpan();
            break;
        case TYPEFACE_BOLD_ITALICS_UNDERLINE:
            ss = new StyleSpan(Typeface.BOLD_ITALIC);
            ss1=new UnderlineSpan();
            break;
        default:
            ss = new StyleSpan(Typeface.NORMAL);
    }
        if(lastCursorPosition>endLength)
            return;
        Log.d(TextArea.class.getSimpleName(), new Integer(lastCursorPosition).toString() + new Integer(endLength).toString());
    if(ss1!=null)
        str.setSpan(ss1, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        str.setSpan(ss, lastCursorPosition, endLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

回答1:


That isn't a function of EditText, its a function of TextWatcher. You aren't making a custom EditText, you're trying to do a quick hack to avoid making a custom EditText. The difference is important because your technique lacks a lot of the power that creating a real custom edit text would have.

Secondly- are you sure the delay isn't in the keyboard? A typical technique for keys who's longpress is different than short press is to delay the action. Many keyboards even allow you to customize the delay (see Swype for example). So it may not be your code delaying it, its the keyboard's built in functionality. (I think this is the most likely answer).

Third- you're doing things really, really inefficiently. You should not be creating new Spans every time this is called. You should be creating 1 set of spans at creation time, and reusing them each time this is called. That alone would give you a good speedup if it is your performance that's the problem.



来源:https://stackoverflow.com/questions/44984121/custom-edittext-freezes-on-clearing-text-using-backspace

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