Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>try this custom ClickableSpan:</p> <pre><code>class MyClickableSpan extends ClickableSpan { private String action; private int fg; private int bg; private boolean selected; public MyClickableSpan(String action, int fg, int bg) { this.action = action; this.fg = fg; this.bg = bg; } @Override public void onClick(View widget) { Log.d(TAG, "onClick " + action); } @Override public void updateDrawState(TextPaint ds) { ds.linkColor = selected? fg : 0xffeeeeee; super.updateDrawState(ds); } } </code></pre> <p>and this SpanWatcher:</p> <pre><code>class Watcher implements SpanWatcher { private TextView tv; private MyClickableSpan selectedSpan = null; public Watcher(TextView tv) { this.tv = tv; } private void changeColor(Spannable text, Object what, int start, int end) { // Log.d(TAG, "changeFgColor " + what); if (what == Selection.SELECTION_END) { MyClickableSpan[] spans = text.getSpans(start, end, MyClickableSpan.class); if (spans != null) { tv.setHighlightColor(spans[0].bg); if (selectedSpan != null) { selectedSpan.selected = false; } selectedSpan = spans[0]; selectedSpan.selected = true; } } } @Override public void onSpanAdded(Spannable text, Object what, int start, int end) { changeColor(text, what, start, end); } @Override public void onSpanChanged(Spannable text, Object what, int ostart, int oend, int nstart, int nend) { changeColor(text, what, nstart, nend); } @Override public void onSpanRemoved(Spannable text, Object what, int start, int end) { } } </code></pre> <p>test it in onCreate:</p> <pre><code> TextView tv = new TextView(this); tv.setTextSize(40); tv.setMovementMethod(LinkMovementMethod.getInstance()); SpannableStringBuilder b = new SpannableStringBuilder(); b.setSpan(new Watcher(tv), 0, 0, Spanned.SPAN_INCLUSIVE_INCLUSIVE); b.append("this is "); int start = b.length(); MyClickableSpan link = new MyClickableSpan("link0 action", 0xffff0000, 0x88ff0000); b.append("link 0"); b.setSpan(link, start, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); b.append("\nthis is "); start = b.length(); b.append("link 1"); link = new MyClickableSpan("link1 action", 0xff00ff00, 0x8800ff00); b.setSpan(link, start, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); b.append("\nthis is "); start = b.length(); b.append("link 2"); link = new MyClickableSpan("link2 action", 0xff0000ff, 0x880000ff); b.setSpan(link, start, b.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(b); setContentView(tv); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload