亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android TextView中識別多個url并分別點擊跳轉方法詳解

發布時間:2020-10-10 15:15:46 來源:腳本之家 閱讀:493 作者:煢&煢 欄目:移動開發

實現方案:

我們直接參考實例代碼:

private String pattern =
    "((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?|(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?";

Pattern r = Pattern.compile(pattern);
Matcher m;
mTv.setText(identifyUrl(richURL.msg));

public SpannableStringBuilderForAllvers identifyUrl(CharSequence text) {
  CharSequence contextText;
  CharSequence clickText;
  text = text == null ? "" : text;
  //以下用于拼接本來存在的spanText
  SpannableStringBuilderForAllvers span = new SpannableStringBuilderForAllvers(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()) {
    //得到網址數m.group()
    if (m.start() < m.end()) {
      span.setSpan(new LinkClickSpan(m.group(), m.group(), mUrlSpanClickListener),
          m.start(), m.end(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
  }
  return span;
}

private static final String HTTPS = "https://";
private static final String HTTP = "http://";
private static final String FTP = "ftp://";

public static boolean hasNetUrlHead(String url) {
  return (!TextUtils.isEmpty(url)) && (url.startsWith(HTTP) || url.startsWith(HTTPS) || url.startsWith(FTP));
}

private UrlSpanClickListener mUrlSpanClickListener = new UrlSpanClickListener() {
  @Override
  public void onClick(View view, String url, String content) {
    if (TextUtils.isEmpty(url)) {
      return;
    }
    Matcher url_matcher = Patterns.WEB_URL.matcher(url);
    if (url_matcher.matches()) {
      String tempUrl;
      if (hasNetUrlHead(url)) {
        tempUrl = url;
      } else {
        tempUrl = HTTPS + url;
      }
      //通過webview打開相應的url

      //Bundle bundle = new Bundle();
      //bundle.putString(WebCordovaBaseFragment.EXTRA_URL, tempUrl);
      //bundle.putBoolean(WebCordovaBaseFragment.ENABLE_WEB_TITLE, true);
      //WebViewActivity.presentWeb(Utilities.getApplicationContext(), WebViewActivity.class, WebCommonFragment.class, content, bundle);
    }
  }
};

public interface UrlSpanClickListener {
  void onClick(View view, String url, String content);
}

public static class LinkClickSpan extends ClickableSpan {
  private int mColor = Utilities.getApplicationContext().getResources().getColor(R.color.yc_color_007AFF_CBN);
  private String mUrl;
  private String mContent;
  UrlSpanClickListener mClickListener;

  public LinkClickSpan(String url, String content, UrlSpanClickListener onClickListener) {
    super();
    mUrl = url;
    mContent = content;
    mClickListener = onClickListener;
  }

  @Override
  public void updateDrawState(TextPaint ds) {
    ds.setColor(mColor);
    ds.linkColor = mColor;
    ds.setUnderlineText(true);//設置是否下劃線
    ds.clearShadowLayer();
  }

  @Override
  public void onClick(View widget) {
    if (mClickListener != null) {
      mClickListener.onClick(widget, mUrl, mContent);
    }
  }
}


public class SpannableStringBuilderForAllvers extends SpannableStringBuilder{

  public SpannableStringBuilderForAllvers() {
    super("");
  }
  public SpannableStringBuilderForAllvers(CharSequence text) {
    super(text, 0, text.length());
  }
  public SpannableStringBuilderForAllvers(CharSequence text, int start, int end){
    super(text,start,end);
  }

  @Override
  public SpannableStringBuilder append(CharSequence text) {
    if (text == null) {
      return this;
    }
    int length = length();
    return (SpannableStringBuilderForAllvers)replace(length, length, text, 0, text.length());
  }

  /**該方法在原API里面只支持API21或者以上,這里適應低版本*/
  public SpannableStringBuilderForAllvers append(CharSequence text, Object what, int flags) {
    if (text == null) {
      return this;
    }
    int start = length();
    append(text);
    setSpan(what, start, length(), flags);
    return this;
  }
}


public class ClickableSpanTextView extends AppCompatTextView {

  private BackgroundColorSpan backgroundColorSpan;
  private boolean hasSpan;

  public ClickableSpanTextView(Context context) {
    super(context);
    init();
  }

  public ClickableSpanTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public ClickableSpanTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    setMovementMethod(LinkMovementMethod.getInstance());
    backgroundColorSpan = new BackgroundColorSpan(getContext().getResources().getColor(R.color.yc_color_4B4B4B_CDG));
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    boolean handled = super.onTouchEvent(event);
    int action = event.getAction();

    if (!(getText() instanceof Spannable)) {
      return handled;
    }

    Spannable spannable = (Spannable) getText();
    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
      int x = (int) event.getX();
      int y = (int) event.getY();
      x -= getTotalPaddingLeft();
      y -= getTotalPaddingTop();
      x += getScrollX();
      y += getScrollY();
      Layout layout = getLayout();
      int line = layout.getLineForVertical(y);
      int off = layout.getOffsetForHorizontal(line, x);
      if (off >= getText().length()) {
        int off1 = layout.getOffsetForHorizontal(line, x - getTextSize());
        if (off == off1) {
          return handled;
        }
      }

      ClickableSpan[] links = spannable.getSpans(off, off, ClickableSpan.class);
      if (links.length > 0) {
        ClickableSpan clickableSpan = links[0];
        int start = spannable.getSpanStart(clickableSpan);
        int end = spannable.getSpanEnd(clickableSpan);

        if (action == MotionEvent.ACTION_DOWN && !hasSpan) {
          spannable.setSpan(backgroundColorSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
          hasSpan = true;
        } else if (hasSpan) {
          spannable.removeSpan(backgroundColorSpan);
          hasSpan = false;
        }
      }
      return links.length != 0;
    } else {
      if (hasSpan && action != MotionEvent.ACTION_MOVE) {
        spannable.removeSpan(backgroundColorSpan);
        hasSpan = false;
      }
    }
    return handled;
  }
}

以上實例代碼大家可以測試下,感謝大家的學習和對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

栾川县| 高安市| 望都县| 淅川县| 洞口县| 雅安市| 上蔡县| 化德县| 遂溪县| 遂川县| 三原县| 新郑市| 吐鲁番市| 原阳县| 景东| 西乌珠穆沁旗| 宜黄县| 嘉黎县| 合作市| 夏津县| 武定县| 巴马| 天津市| 明水县| 白山市| 扬州市| 全南县| 永修县| 绩溪县| 平邑县| 黑龙江省| 清苑县| 凯里市| 英山县| 襄城县| 河西区| 崇州市| 磐安县| 宜宾市| 凤台县| 桂东县|