Select text with multiple EditText controls at the same time

I have the following problem: I'm trying to select text in several EditText elements at the same time by calling viewXYZ.setSelection(int, int) , but the selection is only available in focused view.

Is there a way around this by highlighting text in an unfocused EditText ? Perhaps by overloading the onDraw() methods?

+6
source share
1 answer

I know, but its (as far as I know?) The only way to mark text in an EditText control.

EditText supports Spannable objects, so you can apply primary colors to text (such as background colors) yourself.

This sample project demonstrates a search box that uses the background color for most of the text based on the search results. The key part is the searchFor() method:

  private void searchFor(String text) { TextView prose=(TextView)findViewById(R.id.prose); Spannable raw=new SpannableString(prose.getText()); BackgroundColorSpan[] spans=raw.getSpans(0, raw.length(), BackgroundColorSpan.class); for (BackgroundColorSpan span : spans) { raw.removeSpan(span); } int index=TextUtils.indexOf(raw, text); while (index >= 0) { raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); index=TextUtils.indexOf(raw, text, index + text.length()); } prose.setText(raw); } 

Note that your "output string" should probably be a TextView , not an EditText . EditText is for input, not output.

+4
source

Source: https://habr.com/ru/post/916758/


All Articles