首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Linkify小写URL

Android Linkify小写URL
EN

Stack Overflow用户
提问于 2011-05-02 22:21:33
回答 2查看 1K关注 0票数 0

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

当我将这个url传递给Android Linkify时,结果链接的字母大小写改为http://myserver.com/apps/droidapp.apk

代码语言:javascript
复制
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);

我怎么才能克服这个呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-02 23:36:55

在内部,Linkify调用

代码语言:javascript
复制
public static final boolean addLinks(Spannable s, Pattern p, String scheme, null, null)

检查code for that method

代码语言:javascript
复制
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()位。

票数 3
EN

Stack Overflow用户

发布于 2021-12-28 08:34:07

您可以添加helper函数来更改Linkify修改的urls

代码语言:javascript
复制
/***
   * 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
   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5858306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档