应用程序冻结自动完成textChanged [英] App freezes on autocomplete textChanged

查看:130
本文介绍了应用程序冻结自动完成textChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够获取AutoCompleteTextview的用户输入的第一个和最后一个文本,但是当我在AutocompleteTextView textChanged方法上设置构建器方法时,
我的应用程序冻结。
我感谢所提供的任何努力。

I was able to get the first and last text of user input for AutoCompleteTextview but My App freezes when i set the builder method on AutocompleteTextView textChanged method. I appreciate any effort provided.

@Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if(s.length()>0) {
                String selectedText = s.toString();
                int end = selectedText.length()+start;
                SpannableStringBuilder builder = new SpannableStringBuilder(selectedText);
                builder.setSpan(android.graphics.Typeface.BOLD, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                Log.i("builderText", " "+builder);
                autoCompleteTextView.setText(builder);

            }

        }


推荐答案

问题是你试图在可能的空字符串上获取字符的索引。这就是为什么你得到 -1 这里

The issue is that you are trying to get the index of a character on a possible empty string. That's why you get -1 here

String startText = autoCompleteTextView.getText().toString();
int start = startText.indexOf(0);
int end = startText.indexOf(1);

您可能希望在侦听器中添加该语句,例如 autoCompleteTextView.addTextChangedListener()并在那里处理实际文本更改的位置。

You might want to add that statement in a listener e.g. autoCompleteTextView.addTextChangedListener() and handle it there where the actual text changes occur.

这篇关于应用程序冻结自动完成textChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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