自定义EditText在使用退格键清除文本时冻结 [英] Custom EditText freezes on clearing text using backspace

查看:81
本文介绍了自定义EditText在使用退格键清除文本时冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个自定义EditText,可以接受粗体,斜体,下划线文本. 一切正常,除非我尝试通过长按退格键删除文本.长按退格键会延迟清除文本.

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.

这是重写的onTextChanged()方法

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);
}

推荐答案

这不是EditText的功能,而是TextWatcher的功能.您不是要制作自定义EditText,而是要进行快速修改以避免制作自定义EditText.区别非常重要,因为您的技术缺乏创建真正的自定义编辑文本所具有的强大功能.

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.

其次-您确定延迟不在键盘上吗?长按键与短按键不同的典型技术是延迟操作.许多键盘甚至允许您自定义延迟(例如,参见Swype).因此,可能不是您的代码延迟了它,而是键盘的内置功能. (我认为这是最可能的答案).

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).

第三-您的工作确实非常低效.您不应该在每次调用时都创建新的Span.您应该在创建时创建1组跨度,并在每次调用时重用它们.如果这是性能问题,那么仅此一项就可以使您获得良好的加速效果.

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.

这篇关于自定义EditText在使用退格键清除文本时冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆