android.text.style.ClickableSpan Java Examples

The following examples show how to use android.text.style.ClickableSpan. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextDecorator.java    From text-decorator with Apache License 2.0 8 votes vote down vote up
public TextDecorator makeTextClickable(final OnTextClickListener listener, final int start, final int end, final boolean underlineText) {
  checkIndexOutOfBoundsException(start, end);
  decoratedContent.setSpan(new ClickableSpan() {
    @Override public void onClick(View view) {
      listener.onClick(view, content.substring(start, end));
    }

    @Override public void updateDrawState(TextPaint ds) {
      super.updateDrawState(ds);
      ds.setUnderlineText(underlineText);
    }
  }, start, end, flags);
  textView.setMovementMethod(LinkMovementMethod.getInstance());

  return this;
}
 
Example #2
Source File: PassphraseTypeDialogFragment.java    From delion with Apache License 2.0 6 votes vote down vote up
private SpannableString getResetText() {
    final Context context = getActivity();
    return SpanApplier.applySpans(
            context.getString(R.string.sync_passphrase_encryption_reset_instructions),
            new SpanInfo("<resetlink>", "</resetlink>", new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    Uri syncDashboardUrl = Uri.parse(
                            context.getText(R.string.sync_dashboard_url).toString());
                    Intent intent = new Intent(Intent.ACTION_VIEW, syncDashboardUrl);
                    intent.setPackage(BuildInfo.getPackageName(context));
                    IntentUtils.safePutBinderExtra(
                            intent, CustomTabsIntent.EXTRA_SESSION, null);
                    context.startActivity(intent);
                }
            }));
}
 
Example #3
Source File: TextDrawable.java    From litho with Apache License 2.0 6 votes vote down vote up
public void mount(
    CharSequence text,
    Layout layout,
    float layoutTranslationY,
    ColorStateList colorStateList,
    int userColor,
    int highlightColor,
    ClickableSpan[] clickableSpans) {
  mount(
      text,
      layout,
      0,
      false,
      null,
      userColor,
      highlightColor,
      clickableSpans,
      null,
      null,
      null,
      -1,
      -1,
      0f,
      null);
}
 
Example #4
Source File: LearnMoreTextView.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private void init() {
  setMovementMethod(LinkMovementMethod.getInstance());

  ClickableSpan clickable = new ClickableSpan() {
    @Override
    public void updateDrawState(@NonNull TextPaint ds) {
      super.updateDrawState(ds);
      ds.setUnderlineText(false);
      ds.setColor(ThemeUtil.getThemedColor(getContext(), R.attr.colorAccent));
    }

    @Override
    public void onClick(@NonNull View widget) {
      if (linkListener != null) {
        linkListener.onClick(widget);
      }
    }
  };

  link = new SpannableString(getContext().getString(R.string.LearnMoreTextView_learn_more));
  link.setSpan(clickable, 0, link.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

  visible = true;
}
 
Example #5
Source File: BaseAuthFragment.java    From actor-platform with GNU Affero General Public License v3.0 6 votes vote down vote up
private void findAndHilightTos(SpannableStringBuilder builder, String text, boolean urlAvailable) {
    String tosIndex = getString(R.string.auth_tos_index);
    int index = text.indexOf(tosIndex);
    ClickableSpan span;
    if (urlAvailable) {
        span = new BaseUrlSpan(ActorSDK.sharedActor().getTosUrl(), false);
    } else {
        span = new CustomClicableSpan(new CustomClicableSpan.SpanClickListener() {
            @Override
            public void onClick() {
                new AlertDialog.Builder(getContext())
                        .setTitle(R.string.auth_tos_index)
                        .setMessage(ActorSDK.sharedActor().getTosText())
                        .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .show();
            }
        });
    }
    builder.setSpan(span, index, index + tosIndex.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
 
Example #6
Source File: LoginActivity.java    From FimiX8-RE with MIT License 6 votes vote down vote up
private SpannableString getSpannableString() {
    this.mRegionManage = new RegionManage();
    String string = getString(ServiceItem.getServicename()[this.mRegionManage.getCountryType()]);
    String str1 = this.mContext.getString(R.string.libperson_select);
    String str2 = string;
    SpannableString spannableString = new SpannableString(str1 + str2);
    spannableString.setSpan(new ForegroundColorSpan(this.mContext.getResources().getColor(R.color.libperson_region_label)), 0, str1.length(), 33);
    spannableString.setSpan(new ClickableSpan() {
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(true);
            ds.setColor(LoginActivity.this.mContext.getResources().getColor(R.color.libperson_region));
        }

        public void onClick(View widget) {
        }
    }, str1.length(), str1.length() + str2.length(), 33);
    return spannableString;
}
 
Example #7
Source File: SearchGeolocationDisclosureInfoBar.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
@Override
public void createContent(InfoBarLayout layout) {
    super.createContent(layout);
    SpannableString message = new SpannableString(mMessageText);
    message.setSpan(
            new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    onLinkClicked();
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setUnderlineText(false);
                }
            }, mInlineLinkRangeStart, mInlineLinkRangeEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    layout.setMessage(message);
}
 
Example #8
Source File: QBMessageTextClickMovement.java    From ChatMessagesAdapter-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private String getLinkText(final TextView widget, final Spannable buffer, final MotionEvent event) {

            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

            if (link.length != 0) {
                return buffer.subSequence(buffer.getSpanStart(link[0]),
                        buffer.getSpanEnd(link[0])).toString();
            }

            return buffer.toString();
        }
 
Example #9
Source File: PhysicalWebOptInActivity.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private SpannableString getDescriptionText() {
    return SpanApplier.applySpans(
            getString(R.string.physical_web_optin_description),
            new SpanInfo("<learnmore>", "</learnmore>", new ClickableSpan() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse(PHYSICAL_WEB_LEARN_MORE_URL));
                    // Add the SESSION extra to indicate we want a Chrome custom tab. This
                    // allows the help page to open in the same task as the opt-in activity so
                    // they can share a back stack.
                    String session = null;
                    intent.putExtra(EXTRA_CUSTOM_TABS_SESSION, session);
                    PhysicalWebOptInActivity.this.startActivity(intent);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    // Color links but do not underline them.
                    ds.setColor(ds.linkColor);
                }
            }));
}
 
Example #10
Source File: InfoBarLayout.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Prepares text to be displayed as the infobar's main message, including setting up a
 * clickable link if the infobar requires it.
 */
private CharSequence prepareMainMessageString() {
    SpannableStringBuilder fullString = new SpannableStringBuilder();

    if (mMessageMainText != null) fullString.append(mMessageMainText);

    // Concatenate the text to display for the link and make it clickable.
    if (!TextUtils.isEmpty(mMessageLinkText)) {
        if (fullString.length() > 0) fullString.append(" ");
        int spanStart = fullString.length();

        fullString.append(mMessageLinkText);
        fullString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                mInfoBarView.onLinkClicked();
            }
        }, spanStart, fullString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return fullString;
}
 
Example #11
Source File: AppUtil.java    From droidkaigi2016 with Apache License 2.0 6 votes vote down vote up
public static void linkify(Activity activity, TextView textView, String linkText, String url) {
    String text = textView.getText().toString();

    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(text);
    builder.setSpan(
            new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    showWebPage(activity, url);
                }
            },
            text.indexOf(linkText),
            text.indexOf(linkText) + linkText.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    );

    textView.setText(builder);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
}
 
Example #12
Source File: GalleryCommentsScene.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) {
    Activity activity = getActivity2();
    if (null == activity) {
        return false;
    }

    RecyclerView.ViewHolder holder = parent.getChildViewHolder(view);
    if (holder instanceof CommentHolder) {
        CommentHolder commentHolder = (CommentHolder) holder;
        ClickableSpan span = commentHolder.comment.getCurrentSpan();
        commentHolder.comment.clearCurrentSpan();

        if (span instanceof URLSpan) {
            UrlOpener.openUrl(activity, ((URLSpan) span).getURL(), true);
            return true;
        }
    }

    showCommentDialog(position);

    return true;
}
 
Example #13
Source File: AutofillSaveCardInfoBar.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void createContent(InfoBarLayout layout) {
    super.createContent(layout);
    InfoBarControlLayout control = layout.addControlLayout();
    for (int i = 0; i < mCardDetails.size(); i++) {
        CardDetail detail = mCardDetails.get(i);
        control.addIcon(detail.issuerIconDrawableId, 0, detail.label, detail.subLabel);
    }

    for (LegalMessageLine line : mLegalMessageLines) {
        SpannableString text = new SpannableString(line.text);
        for (final LegalMessageLine.Link link : line.links) {
            text.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    nativeOnLegalMessageLinkClicked(mNativeAutofillSaveCardInfoBar, link.url);
                }
            }, link.start, link.end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        control.addDescription(text);
    }
}
 
Example #14
Source File: AdapterActManage.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder,
                              final URLSpan urlSpan) {
    int start = clickableHtmlBuilder.getSpanStart(urlSpan);
    int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
    int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
    clickableHtmlBuilder.setSpan(new ClickableSpan() {
        public void onClick(View view) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(urlSpan.getURL()));
                context.startActivity(intent);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }, start, end, flags);
}
 
Example #15
Source File: AutofillSaveCardInfoBar.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
@Override
public void createContent(InfoBarLayout layout) {
    super.createContent(layout);
    InfoBarControlLayout control = layout.addControlLayout();
    for (int i = 0; i < mCardDetails.size(); i++) {
        CardDetail detail = mCardDetails.get(i);
        control.addIcon(detail.issuerIconDrawableId, 0, detail.label, detail.subLabel);
    }

    for (LegalMessageLine line : mLegalMessageLines) {
        SpannableString text = new SpannableString(line.text);
        for (final LegalMessageLine.Link link : line.links) {
            text.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View view) {
                    nativeOnLegalMessageLinkClicked(mNativeAutofillSaveCardInfoBar, link.url);
                }
            }, link.start, link.end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        control.addDescription(text);
    }
}
 
Example #16
Source File: TweetTextLinkifierTest.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testLinkifyUrls_linkClickListener() {
    final String url = "http://t.co/foo";
    final String displayUrl = "dev.twitter.com";
    final String fullText = BASE_TEXT + " " + "http://t.co/foo";

    final LinkClickListener mockClickListener = mock(LinkClickListener.class);

    final UrlEntity urlEntity =
            EntityFactory.newUrlEntity(fullText, url, displayUrl);
    final FormattedTweetText formattedText = new FormattedTweetText();
    formattedText.text = fullText;
    formattedText.urlEntities.add(FormattedUrlEntity.createFormattedUrlEntity(urlEntity));

    final SpannableStringBuilder linkifiedText = (SpannableStringBuilder)
            TweetTextLinkifier.linkifyUrls(formattedText, mockClickListener, 0, 0, true,
                    true);
    final ClickableSpan[] clickables =
            linkifiedText.getSpans(urlEntity.getStart(), urlEntity.getEnd(),
                    ClickableSpan.class);
    assertEquals(1, clickables.length);
}
 
Example #17
Source File: HelperUrl.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void insertAtSignLink(final String text, SpannableStringBuilder builder, int start) {

        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                G.isLinkClicked = true;
                checkUsernameAndGoToRoom(text, ChatEntry.profile);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
//                if (G.isDarkTheme) {
//                    ds.linkColor = LinkColorDark;
//                } else {
//                    ds.linkColor = LinkColor;
//                }
                ds.linkColor = Color.parseColor(G.linkColor);

                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        };

        builder.setSpan(clickableSpan, start, start + text.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
 
Example #18
Source File: GetWordTextView.java    From allenglish with Apache License 2.0 6 votes vote down vote up
private ClickableSpan getClickableSpan(final String word) {
    return new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            if (!word.matches("^[A-Za-z\\-']+$")) {
                return; // 不是英文单词
            }
            if (mOnWordClickListener != null) {
                mOnWordClickListener.onClick(word);
            }
        }

        @Override
        public void updateDrawState(TextPaint ds) {
        }

    };
}
 
Example #19
Source File: ConfirmInfoBar.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public CharSequence getMessageText(Context context) {
    // Construct text to be displayed on the infobar.
    SpannableStringBuilder infobarMessage = new SpannableStringBuilder(mMessage);

    // If we have a link text to display, append it.
    if (!TextUtils.isEmpty(mLinkText)) {
        SpannableStringBuilder spannableLinkText = new SpannableStringBuilder(mLinkText);
        ClickableSpan onLinkClicked = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                onLinkClicked();
            }
        };
        spannableLinkText.setSpan(onLinkClicked, 0, spannableLinkText.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        infobarMessage.append(" ");
        infobarMessage.append(spannableLinkText);
    }
    return infobarMessage;
}
 
Example #20
Source File: ChatAdapter.java    From Twire with GNU General Public License v3.0 6 votes vote down vote up
private void checkForLink(String message, SpannableStringBuilder spanbuilder) {
    Matcher linkMatcher = Patterns.WEB_URL.matcher(message);
    while (linkMatcher.find()) {
        String url = linkMatcher.group(0);

        if (!url.matches("^https?://.+"))
            url = "http://" + url;

        final String finalUrl = url;
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                CustomTabsIntent.Builder mTabs = new CustomTabsIntent.Builder();
                mTabs.setStartAnimations(context, R.anim.slide_in_bottom_anim, R.anim.fade_out_semi_anim);
                mTabs.setExitAnimations(context, R.anim.fade_in_semi_anim, R.anim.slide_out_bottom_anim);
                mTabs.build().launchUrl(context, Uri.parse(finalUrl));

                mRecyclerView.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
            }
        };

        spanbuilder.setSpan(clickableSpan, linkMatcher.start(), linkMatcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
 
Example #21
Source File: MainActivity.java    From RefreashTabView with Apache License 2.0 6 votes vote down vote up
private void initDesc() {
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
    stringBuilder.append(getString(R.string.head_title_desc));
    String link = getString(R.string.head_title_desc_link);
    SpannableString span = new SpannableString(link);
    span.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            //
            Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_LONG).show();
        }
    }, 0, link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    stringBuilder.append(span);
    stringBuilder.append(" ");
    descTextView.setText(stringBuilder);
    //descTextView.setMovementMethod(LinkMovementMethod.getInstance());
    // fix TextView has LinkMovementMethod intercept Scroll event
    descTextView.setOnTouchListener(LinkMovement.getInstance());
}
 
Example #22
Source File: DuplicateDownloadInfoBar.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the infobar text for offline page downloads.
 * @param context Context to be used.
 * @param template Template of the text to be displayed.
 */
private CharSequence getOfflinePageMessageText(final Context context, final String template) {
    return getMessageText(template, mFilePath, new ClickableSpan() {
        @Override
        public void onClick(View view) {
            // TODO(qinmin): open the offline page on local storage instead of opening the url.
            // However, there could be multiple stored offline pages for the same url, need to
            // figure out which one to use.
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(mPageUrl));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setPackage(context.getPackageName());
            context.startActivity(intent);
        }
    });
}
 
Example #23
Source File: PermissionDialogController.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private CharSequence prepareMainMessageString(final PermissionDialogDelegate delegate) {
    SpannableStringBuilder fullString = new SpannableStringBuilder();

    String messageText = delegate.getMessageText();
    String linkText = delegate.getLinkText();
    if (!TextUtils.isEmpty(messageText)) fullString.append(messageText);

    // If the linkText exists, then wrap it in a clickable span and concatenate it with the main
    // dialog message.
    if (!TextUtils.isEmpty(linkText)) {
        if (fullString.length() > 0) fullString.append(" ");
        int spanStart = fullString.length();

        fullString.append(linkText);
        fullString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                mDecision = NOT_DECIDED;
                delegate.onLinkClicked();
                if (mDialog != null) mDialog.dismiss();
            }
        }, spanStart, fullString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return fullString;
}
 
Example #24
Source File: FormatTextView.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public void setText(CharSequence text, BufferType type) {
    Spanned html = Html.fromHtml(text.toString());
    ClickableSpan[] spans = html.getSpans(0, html.length(), ClickableSpan.class);
    if (spans != null && spans.length > 0) {
        setMovementMethod(LinkMovementMethod.getInstance());
    }
    super.setText(html, type);
}
 
Example #25
Source File: StringUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
public static SpannableString generateTvSpreadSpanText(String text1, String text2, ClickableSpan clickableSpan) {
    if (TextUtils.isEmpty(text1)) {
        text1 = "";
    }
    String str = text1;
    if (!TextUtils.isEmpty(text2)) {
        str = text1 + text2;
    }
    SpannableString spannableString = new SpannableString(str);
    if (clickableSpan != null) {
        spannableString.setSpan(clickableSpan, text1.length(), str.length(), 33);
    }
    return spannableString;
}
 
Example #26
Source File: CommentListView.java    From TestChat with Apache License 2.0 5 votes vote down vote up
private SpannableString getClickableSpan(final String name, final String uid) {
        SpannableString spannableString = new SpannableString(name);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#232121")), 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                        if (mCommentItemClickListener != null) {
                                LogUtil.e("点击了评论的belong用户");
                                mCommentItemClickListener.onCommentItemNameClick(uid);
                        }
                }
        }, 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
}
 
Example #27
Source File: LinkifyTextView.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    // Let the parent or grandparent of TextView to handles click aciton.
    // Otherwise click effect like ripple will not work, and if touch area
    // do not contain a url, the TextView will still get MotionEvent.
    // onTouchEven must be called with MotionEvent.ACTION_DOWN for each touch
    // action on it, so we analyze touched url here.
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mCurrentSpan = null;

        if (getText() instanceof Spanned) {
            // Get this code from android.text.method.LinkMovementMethod.
            // Work fine !
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= getTotalPaddingLeft();
            y -= getTotalPaddingTop();

            x += getScrollX();
            y += getScrollY();

            Layout layout = getLayout();
            if (null != layout) {
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);

                ClickableSpan[] spans = ((Spanned)getText()).getSpans(off, off, ClickableSpan.class);

                if (spans.length > 0) {
                    mCurrentSpan = spans[0];
                }
            }
        }
    }

    return super.onTouchEvent(event);
}
 
Example #28
Source File: TextDrawable.java    From litho with Apache License 2.0 5 votes vote down vote up
private static boolean containsLongClickableSpan(@Nullable ClickableSpan[] clickableSpans) {
  if (clickableSpans == null) {
    return false;
  }

  for (ClickableSpan span : clickableSpans) {
    if (span instanceof LongClickableSpan) {
      return true;
    }
  }

  return false;
}
 
Example #29
Source File: SpanUtils.java    From styT with Apache License 2.0 5 votes vote down vote up
public static SpannableString getClickableSpan(final Context context) {
    SpannableString spannableString = new SpannableString("哪里不会点哪里,so easy!");
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(context, "你戳中我了", Toast.LENGTH_SHORT).show();
        }
    };
    spannableString.setSpan(clickableSpan, 5, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    return spannableString;
}
 
Example #30
Source File: LinkableTextView.java    From px-android with MIT License 5 votes vote down vote up
private void addLinkToSpannable(@NonNull final Spannable spannable, @NonNull final LinkablePhrase link) {
    final String phrase = link.getPhrase();
    final int start = TextUtil.isNotEmpty(phrase) ? model.getText().indexOf(phrase) : -1;
    if (start >= 0) {
        final int end = start + phrase.length();
        spannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull final View widget) {
                onLinkClicked(link);
            }
        }, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ViewUtils.setColorInSpannable(link.getTextColor(), start, end, spannable);
    }
}