【问题标题】:URL Formatting in TextViewTextView 中的 URL 格式
【发布时间】:2017-04-28 16:59:52
【问题描述】:

所以在我的 API 中,用户拥有[[www.google.com Google]] 格式的网址,其中Google 将链接到该网址。我一直试图弄清楚如何写这个有一段时间,但无法弄清楚如何去做。

谁能帮忙?

【问题讨论】:

  • 你尝试了什么?
  • @limonik 我已经尝试将 replaceAll 设为 HTML 链接,但我得到的是 <a href="www.google.com Google">www.google.com Google</a>,这显然是我不想要的

标签: android url hyperlink textview


【解决方案1】:

你应该试试 ClickSpan。

try {
        String spannableText = getString(
                R.string.y);
        termsOfService.setText(spannableText);
        int i1 = string.indexOf(spannableText);
        int i2 = i1 + spannableText.length();
        if (i1 > 0 && i2 > 0) {
            Spannable wordToSpan = new SpannableString(string);
            ClickSpan clickableSpan = new ClickSpan(getActivity());
            wordToSpan.setSpan(clickableSpan, i1, i2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            termsOfService.setMovementMethod(LinkMovementMethod.getInstance());
            termsOfService.setText(wordToSpan);
        }

    } catch (Exception ex) {
        if (ex != null)
            ex.printStackTrace();
    }

参考在:https://developer.android.com/reference/android/text/style/ClickableSpan.html 祝你好运。

【讨论】:

  • 它出来的格式是<a href="www.google.com Google">www.google.com Google</a>ClickSpan 帮助解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
相关资源
最近更新 更多