问题
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);
You might want to add that statement in a listener e.g. autoCompleteTextView.addTextChangedListener()
and handle it there where the actual text changes occur.
来源:https://stackoverflow.com/questions/41961050/app-freezes-on-autocomplete-textchanged