Android Linkify 小写 URL

发布于 2024-11-04 13:24:25 字数 729 浏览 0 评论 0 原文

我有一个带有一些大写字母的 URL,例如 http://myserver.com/apps/DroidApp.apk

当我将此 url 传递给 Android Linkify,生成的链接字母大小写更改为 http://myserver.com/apps/droidapp.apk< /code>

TextView upgradeLink = (TextView) findViewById(R.id.upgradeNow);
upgradeLink.setText("Download now.");
Pattern pattern = Pattern.compile("Download");
String scheme = "http://myserver.com/apps/DroidApp.apk+"?action=";
Log.e(MY_DEBUG_TAG,"Schema URL "+scheme); // printed http://myserver.com/apps/DroidApp.apk?action=
Linkify.addLinks(upgradeLink, pattern, scheme);

我该如何克服这个问题?

I have a URL with some Upper case letters like http://myserver.com/apps/DroidApp.apk

When I passed this url to Android Linkify, the resulting link's letter case changed to http://myserver.com/apps/droidapp.apk

TextView upgradeLink = (TextView) findViewById(R.id.upgradeNow);
upgradeLink.setText("Download now.");
Pattern pattern = Pattern.compile("Download");
String scheme = "http://myserver.com/apps/DroidApp.apk+"?action=";
Log.e(MY_DEBUG_TAG,"Schema URL "+scheme); // printed http://myserver.com/apps/DroidApp.apk?action=
Linkify.addLinks(upgradeLink, pattern, scheme);

How can I over come this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赴月观长安 2024-11-11 13:24:25

在内部,Linkify 正在调用

public static final boolean addLinks(Spannable s, Pattern p, String scheme, null, null)

检查 该方法的代码

public static final boolean addLinks(Spannable s, Pattern p,
        String scheme, MatchFilter matchFilter,
        TransformFilter transformFilter) {
    boolean hasMatches = false;
    String prefix = (scheme == null) ? "" : scheme.toLowerCase(); // <-- here is the problem!
    Matcher m = p.matcher(s);

    while (m.find()) {
        int start = m.start();
        int end = m.end();
        boolean allowed = true;

        if (matchFilter != null) {
            allowed = matchFilter.acceptMatch(s, start, end);
        }

        if (allowed) {
            String url = makeUrl(m.group(0), new String[] { prefix },
                                 m, transformFilter);

            applyLink(url, start, end, s);
            hasMatches = true;
        }
    }

    return hasMatches;
}

扩展 Linkify 并覆盖此方法,删除 scheme.toLowerCase() 位。

Internally, Linkify is calling

public static final boolean addLinks(Spannable s, Pattern p, String scheme, null, null)

Check the code for that method:

public static final boolean addLinks(Spannable s, Pattern p,
        String scheme, MatchFilter matchFilter,
        TransformFilter transformFilter) {
    boolean hasMatches = false;
    String prefix = (scheme == null) ? "" : scheme.toLowerCase(); // <-- here is the problem!
    Matcher m = p.matcher(s);

    while (m.find()) {
        int start = m.start();
        int end = m.end();
        boolean allowed = true;

        if (matchFilter != null) {
            allowed = matchFilter.acceptMatch(s, start, end);
        }

        if (allowed) {
            String url = makeUrl(m.group(0), new String[] { prefix },
                                 m, transformFilter);

            applyLink(url, start, end, s);
            hasMatches = true;
        }
    }

    return hasMatches;
}

Extend Linkify and override this method, removing the scheme.toLowerCase() bit.

追我者格杀勿论 2024-11-11 13:24:25

您可以添加辅助函数来更改 Linkify 修改的 url

/***
   * hyperlinks @toThis to @linkThis in the given @textView
***/
fun addLinks(textView: TextView, linkThis: String, toThis: String) {
   val pattern = Pattern.compile(linkThis, Pattern.CASE_INSENSITIVE)
   android.text.util.Linkify.addLinks(textView, pattern, toThis, { s, start, end -> true }, { match, url -> "" })
  //Maintain url case
  val spanText = textView.text
  if(spanText !is Spannable) return // no need to process since there are no URLSpans
      val matcher = pattern.matcher(linkThis)
      while (matcher.find()) {
      val span = spanText.getSpans(matcher.start(), matcher.end(), URLSpan::class.java).first()
      val spanFlags = spanText.getSpanFlags(span)
      spanText.removeSpan(span) //remove modified url added by linkify
      spanText.setSpan(URLSpan(toThis), matcher.start(), matcher.end(), spanFlags) //add the original url
      }
      textView.text = spanText
   }

You can add helper function to change the urls modified by Linkify

/***
   * hyperlinks @toThis to @linkThis in the given @textView
***/
fun addLinks(textView: TextView, linkThis: String, toThis: String) {
   val pattern = Pattern.compile(linkThis, Pattern.CASE_INSENSITIVE)
   android.text.util.Linkify.addLinks(textView, pattern, toThis, { s, start, end -> true }, { match, url -> "" })
  //Maintain url case
  val spanText = textView.text
  if(spanText !is Spannable) return // no need to process since there are no URLSpans
      val matcher = pattern.matcher(linkThis)
      while (matcher.find()) {
      val span = spanText.getSpans(matcher.start(), matcher.end(), URLSpan::class.java).first()
      val spanFlags = spanText.getSpanFlags(span)
      spanText.removeSpan(span) //remove modified url added by linkify
      spanText.setSpan(URLSpan(toThis), matcher.start(), matcher.end(), spanFlags) //add the original url
      }
      textView.text = spanText
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文