|
8 | 8 | import java.util.List;
|
9 | 9 |
|
10 | 10 | import android.content.res.Resources;
|
| 11 | +import android.text.Spannable; |
| 12 | +import android.text.SpannableString; |
| 13 | +import android.text.Spanned; |
| 14 | +import android.text.method.LinkMovementMethod; |
| 15 | +import android.text.method.MovementMethod; |
| 16 | +import android.text.style.ClickableSpan; |
| 17 | +import android.view.View; |
| 18 | +import android.widget.TextView; |
11 | 19 |
|
12 | 20 | public class StringUtils {
|
13 | 21 |
|
@@ -64,5 +72,32 @@ public static String[] intersection(String[] a, String[] b){
|
64 | 72 | retval.toArray(retarray);
|
65 | 73 | return retarray;
|
66 | 74 | }
|
| 75 | + |
| 76 | + public abstract static class ClickSpan extends ClickableSpan { |
| 77 | + |
| 78 | + } |
| 79 | + public static void clickify(TextView view, final String clickableText, |
| 80 | + final ClickSpan span) { |
| 81 | + |
| 82 | + CharSequence text = view.getText(); |
| 83 | + String string = text.toString(); |
| 84 | + |
| 85 | + int start = string.indexOf(clickableText); |
| 86 | + int end = start + clickableText.length(); |
| 87 | + if (start == -1) return; |
| 88 | + |
| 89 | + if (text instanceof Spannable) { |
| 90 | + ((Spannable)text).setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 91 | + } else { |
| 92 | + SpannableString s = SpannableString.valueOf(text); |
| 93 | + s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 94 | + view.setText(s); |
| 95 | + } |
| 96 | + |
| 97 | + MovementMethod m = view.getMovementMethod(); |
| 98 | + if ((m == null) || !(m instanceof LinkMovementMethod)) { |
| 99 | + view.setMovementMethod(LinkMovementMethod.getInstance()); |
| 100 | + } |
| 101 | + } |
67 | 102 |
|
68 | 103 | }
|
0 commit comments