【问题标题】:Android Linkify lower cases the URLAndroid Linkify 小写 URL
【发布时间】:2011-05-02 14:21:32
【问题描述】:

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

当我将此 url 传递给 Android Linkify 时,生成的链接的字母大小写更改为 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);

我该如何克服?

【问题讨论】:

    标签: android linkify


    【解决方案1】:

    在内部,Linkify 正在调用

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

    查看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;
    }
    

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

    【讨论】:

      【解决方案2】:

      你可以添加帮助函数来改变被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
         }
      

      【讨论】:

        猜你喜欢
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-23
        • 2019-10-21
        • 1970-01-01
        • 2016-02-04
        • 1970-01-01
        相关资源
        最近更新 更多