【问题标题】:AutoLink link square brackets of URL in TextViewTextView中URL的AutoLink链接方括号
【发布时间】:2014-03-20 06:47:50
【问题描述】:

我有一个网址: https://<site name>/pallavi/[Songs.PK]%2002%20.mp3

我有一个文本视图,属性为:android:autoLink="all"

如果我只是将文本设置为文本视图,我的文本视图只会突出显示 [.它看起来像这样:

https://< site name >/pallavi/[Songs.PK]%2002%20.mp3

我想要的是,整个链接应该像这样突出显示:

https://< site name >/pallavi/[Songs.PK]%2002%20.mp3

到目前为止我所尝试的:

  1. 使用了

    标签和Html.fromHtml,但是好像不行! (我什至不知道android是否支持
    。)
  2. 使用了 Jsoup.parser。但这似乎对我也不起作用。

更新 我也试过这个答案:https://stackoverflow.com/a/12376115/1320263


如果问题出在 android 上,请告诉我文本视图的 linkAll 属性本身是否不将括号视为有效字符?如果它被支持,我该如何超链接呢?

另请注意: 我在问题中写的文本(或链接)只是一个示例文本。实际上,我得到了一段文本,从那里很难确定超链接的确切开始位置和结束位置。此外,块中存在的链接数量将是未知的。因此我不能使用&lt; a href = "" &gt; 的东西...

【问题讨论】:

标签: android android-layout textview android-textattributes


【解决方案1】:

如果其他人碰巧有同样的问题,以下是对我有用的解决方案:

    Pattern pattern = Pattern.compile("(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))");


    SpannableString spannable = new SpannableString(html);
    Matcher matcher = pattern.matcher(spannable);

    // Create ActivitySpans for each match
    while (matcher.find())
        spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Create a new TextView with these spans and enable the clickable links
    mTxtEventDescription.setText(spannable);

【讨论】:

  • 你到底是怎么想出那些长长的正则表达式的? :O
  • 我完全不知道我当时是怎么做到的...... :)
【解决方案2】:

你可以试试这个:

((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));`

【讨论】:

猜你喜欢
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
相关资源
最近更新 更多