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 Project: text-decorator   Author: nntuyen   File: TextDecorator.java    License: 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 Project: mollyim-android   Author: mollyim   File: LearnMoreTextView.java    License: 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 #3
Source Project: FimiX8-RE   Author: wladimir-computin   File: LoginActivity.java    License: 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 #4
Source Project: AndroidChromium   Author: JackyAndroid   File: SearchGeolocationDisclosureInfoBar.java    License: 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 #5
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 #6
Source Project: AndroidChromium   Author: JackyAndroid   File: PhysicalWebOptInActivity.java    License: 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 #7
Source Project: MHViewer   Author: axlecho   File: GalleryCommentsScene.java    License: 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 #8
Source Project: RefreashTabView   Author: FreeSunny   File: MainActivity.java    License: 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 #9
Source Project: Twire   Author: Perflyst   File: ChatAdapter.java    License: 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 #10
Source Project: AndroidChromium   Author: JackyAndroid   File: AutofillSaveCardInfoBar.java    License: 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 #11
Source Project: delion   Author: derry   File: PassphraseTypeDialogFragment.java    License: 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 #12
Source Project: twitter-kit-android   Author: twitter-archive   File: TweetTextLinkifierTest.java    License: 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 #13
Source Project: allenglish   Author: veeson   File: GetWordTextView.java    License: 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 #14
Source Project: 365browser   Author: mogoweb   File: AutofillSaveCardInfoBar.java    License: 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 #15
Source Project: AndroidChromium   Author: JackyAndroid   File: PermissionDialogController.java    License: 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 #16
Source Project: droidkaigi2016   Author: konifar   File: AppUtil.java    License: 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 #17
Source Project: actor-platform   Author: actorapp   File: BaseAuthFragment.java    License: 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 #18
Source Project: litho   Author: facebook   File: TextDrawable.java    License: 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 #19
Source Project: BigApp_Discuz_Android   Author: BigAppOS   File: AdapterActManage.java    License: 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 #20
Source Project: delion   Author: derry   File: InfoBarLayout.java    License: 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 #21
Source Project: iGap-Android   Author: RooyeKhat-Media   File: HelperUrl.java    License: 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 #22
Source Project: android-chromium   Author: kevin-smets   File: ConfirmInfoBar.java    License: 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 #23
Source Project: 365browser   Author: mogoweb   File: DuplicateDownloadInfoBar.java    License: 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 #24
Source Project: AndroidChromium   Author: JackyAndroid   File: DuplicateDownloadInfoBar.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to get the text to be displayed on the infobar.
 * @param template Message template.
 * @param fileName Name of the file.
 * @param clickableSpan Action to perform when clicking on the file name.
 * @return message to be displayed on the infobar.
 */
private CharSequence getMessageText(final String template, final String fileName,
        final ClickableSpan clickableSpan) {
    final SpannableString formattedFilePath = new SpannableString(fileName);
    formattedFilePath.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    formattedFilePath.setSpan(clickableSpan, 0, fileName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return TextUtils.expandTemplate(template, formattedFilePath);
}
 
Example #25
Source Project: weather   Author: bkhezry   File: AppUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set text of textView with html format of html parameter
 *
 * @param textView instance {@link TextView}
 * @param html     String
 */
@SuppressLint("ClickableViewAccessibility")
public static void setTextWithLinks(TextView textView, CharSequence html) {
  textView.setText(html);
  textView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      int action = event.getAction();
      if (action == MotionEvent.ACTION_UP ||
          action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        TextView widget = (TextView) v;
        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 = Spannable.Factory.getInstance()
            .newSpannable(widget.getText())
            .getSpans(off, off, ClickableSpan.class);

        if (link.length != 0) {
          if (action == MotionEvent.ACTION_UP) {
            link[0].onClick(widget);
          }
          return true;
        }
      }
      return false;
    }
  });
}
 
Example #26
Source Project: star-zone-android   Author: bootsrc   File: ExTextView.java    License: Apache License 2.0 5 votes vote down vote up
private SpannableStringBuilderCompat recognUrl(CharSequence text) {
    mStringList.clear();
    mUrlInfos.clear();

    CharSequence contextText;
    CharSequence clickText;
    text = text == null ? "" : text;
    //以下用于拼接本来存在的spanText
    SpannableStringBuilderCompat span = new SpannableStringBuilderCompat(text);
    ClickableSpan[] clickableSpans = span.getSpans(0, text.length(), ClickableSpan.class);
    if (clickableSpans.length > 0) {
        int start = 0;
        int end = 0;
        for (int i = 0; i < clickableSpans.length; i++) {
            start = span.getSpanStart(clickableSpans[0]);
            end = span.getSpanEnd(clickableSpans[i]);
        }
        //可点击文本后面的内容页
        contextText = text.subSequence(end, text.length());
        //可点击文本
        clickText = text.subSequence(start,
                end);
    } else {
        contextText = text;
        clickText = null;
    }
    m = r.matcher(contextText);
    //匹配成功
    while (m.find()) {
        //得到网址数
        UrlInfo info = new UrlInfo();
        info.start = m.start();
        info.end = m.end();
        mStringList.add(m.group());
        mUrlInfos.add(info);
    }
    return jointText(clickText, contextText);
}
 
Example #27
Source Project: Klyph   Author: jonathangerbaud   File: LinkMovementMethod.java    License: MIT License 5 votes vote down vote up
@Override
public boolean  onTouchEvent(TextView widget, Spannable buffer, MotionEvent event)
{
	int action = event.getAction();

       if (action == MotionEvent.ACTION_UP ||
           action == MotionEvent.ACTION_DOWN) {
           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) {
               if (action == MotionEvent.ACTION_UP) {
                   link[0].onClick(widget);
               } else if (action == MotionEvent.ACTION_DOWN) {
                   /*Selection.setSelection(buffer,
                                          buffer.getSpanStart(link[0]),
                                          buffer.getSpanEnd(link[0]));*/
               }

               return false;
           } else {
               //Selection.removeSelection(buffer);
           }
       }

       //return super.onTouchEvent(widget, buffer, event);
	return false;
}
 
Example #28
Source Project: android_9.0.0_r45   Author: lulululbj   File: AccessibilityNodeInfo.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the text of this node.
 * <p>
 *   <strong>Note:</strong> Cannot be called from an
 *   {@link android.accessibilityservice.AccessibilityService}.
 *   This class is made immutable before being delivered to an AccessibilityService.
 * </p>
 *
 * @param text The text.
 *
 * @throws IllegalStateException If called from an AccessibilityService.
 */
public void setText(CharSequence text) {
    enforceNotSealed();
    mOriginalText = text;
    // Replace any ClickableSpans in mText with placeholders
    if (text instanceof Spanned) {
        ClickableSpan[] spans =
                ((Spanned) text).getSpans(0, text.length(), ClickableSpan.class);
        if (spans.length > 0) {
            Spannable spannable = new SpannableStringBuilder(text);
            for (int i = 0; i < spans.length; i++) {
                ClickableSpan span = spans[i];
                if ((span instanceof AccessibilityClickableSpan)
                        || (span instanceof AccessibilityURLSpan)) {
                    // We've already done enough
                    break;
                }
                int spanToReplaceStart = spannable.getSpanStart(span);
                int spanToReplaceEnd = spannable.getSpanEnd(span);
                int spanToReplaceFlags = spannable.getSpanFlags(span);
                spannable.removeSpan(span);
                ClickableSpan replacementSpan = (span instanceof URLSpan)
                        ? new AccessibilityURLSpan((URLSpan) span)
                        : new AccessibilityClickableSpan(span.getId());
                spannable.setSpan(replacementSpan, spanToReplaceStart, spanToReplaceEnd,
                        spanToReplaceFlags);
            }
            mText = spannable;
            return;
        }
    }
    mText = (text == null) ? null : text.subSequence(0, text.length());
}
 
Example #29
Source Project: android_9.0.0_r45   Author: lulululbj   File: AccessibilityInteractionController.java    License: Apache License 2.0 5 votes vote down vote up
private boolean handleClickableSpanActionUiThread(
        View view, int virtualDescendantId, Bundle arguments) {
    Parcelable span = arguments.getParcelable(ACTION_ARGUMENT_ACCESSIBLE_CLICKABLE_SPAN);
    if (!(span instanceof AccessibilityClickableSpan)) {
        return false;
    }

    // Find the original ClickableSpan if it's still on the screen
    AccessibilityNodeInfo infoWithSpan = null;
    AccessibilityNodeProvider provider = view.getAccessibilityNodeProvider();
    if (provider != null) {
        infoWithSpan = provider.createAccessibilityNodeInfo(virtualDescendantId);
    } else if (virtualDescendantId == AccessibilityNodeProvider.HOST_VIEW_ID) {
        infoWithSpan = view.createAccessibilityNodeInfo();
    }
    if (infoWithSpan == null) {
        return false;
    }

    // Click on the corresponding span
    ClickableSpan clickableSpan = ((AccessibilityClickableSpan) span).findClickableSpan(
            infoWithSpan.getOriginalText());
    if (clickableSpan != null) {
        clickableSpan.onClick(view);
        return true;
    }
    return false;
}
 
Example #30
Source Project: 365browser   Author: mogoweb   File: TextViewWithClickableSpans.java    License: Apache License 2.0 5 votes vote down vote up
private void handleAccessibilityClick() {
    ClickableSpan[] clickableSpans = getClickableSpans();
    if (clickableSpans == null || clickableSpans.length == 0) {
        return;
    } else if (clickableSpans.length == 1) {
        clickableSpans[0].onClick(this);
    } else {
        openDisambiguationMenu();
    }
}