Xamarin:无法从带有链接的 TextView 中检索 URLSpans [英] Xamarin: Unable to retrieve URLSpans from a TextView with links

查看:12
本文介绍了Xamarin:无法从带有链接的 TextView 中检索 URLSpans的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Xamarin 项目中的 textView 中删除链接下划线.以下代码段在 Java 项目中运行良好:

I'm trying to remove link underlines from textView in a Xamarin projects. The following snippet works well in a java project:

    textView.setText("You must agree to our terms and conditions");

    Pattern pattern1 = Pattern.compile("terms and conditions");
    Linkify.addLinks(textView, pattern1, "http://www.someLink.com", null, new Linkify.TransformFilter() {
        @Override
        public String transformUrl(Matcher match, String url) {
            return "";
        }
    });

    Spannable s = new SpannableString(textView.getText());
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span: spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        NoUnderline noUnderline = new NoUnderline();
        s.setSpan(noUnderline, start, end, 0);
    }
    textView.setText(s);

现在,在 C# 中翻译完全相同的代码,我得出以下结论:

Now, translating the exact same code in C#, I came to the following:

        TextView.Text = "You must agree to our terms and conditions";

        Java.Util.Regex.Pattern termsAndConditionsMatcher = Java.Util.Regex.Pattern.Compile("terms and conditions");
        Linkify.AddLinks(TextView, termsAndConditionsMatcher, "https://www.someLink.com/", null, new EmptyLinkTransformer());

        var spannable = new SpannableString(TextView.Text);
        var spans = spannable.GetSpans(0, spannable.Length(), Java.Lang.Class.FromType(typeof(URLSpan)));

        var urlSpans = new URLSpan[spans.Length];
        for (var i = 0; i < urlSpans.Length; i++)
        {
            urlSpans[i] = spans[i] as URLSpan;
        }

        foreach (URLSpan span in urlSpans)
        {
            int start = spannable.GetSpanStart(span);
            int end = spannable.GetSpanEnd(span);
            var updated = new NoUnderlineSpan();
            spannable.SetSpan(updated, start, end, 0);
        }

        TextView.TextFormatted = spannable;

问题是 spannable.GetSpans(0, spannable.Length(), Java.Lang.Class.FromType(typeof(URLSpan))) 返回 0 个元素,即使我看到了链接在 UI 上有颜色和下划线.

The problem is that spannable.GetSpans(0, spannable.Length(), Java.Lang.Class.FromType(typeof(URLSpan))) returns 0 elements, even though I see the links being colored and underlined on the UI.

这个这篇 帖子表明我正确使用了该方法.我在这里做错了吗?

This and this post suggest that I'm using the method correctly. Am I doing something wrong here?

推荐答案

Xamarin:无法从带有链接的 TextView 中检索 URLSpans

Xamarin: Unable to retrieve URLSpans from a TextView with links

这是一种解决方法:您可以使用 ClickableSpan 而不是 URLSpan 来实现相同的功能.

Here is a workaround: You could use ClickableSpan instead of URLSpan to implement the same feature.

自定义一个 ClickableSpan 类并覆盖 UpdateDrawState 以从超链接 TextView 中删除下划线:

Custom a ClickableSpan class and override the UpdateDrawState to remove underline from the hyperlink TextView :

class MyClickableSpan : ClickableSpan
{
    private MainActivity mainActivity;

    public MyClickableSpan(MainActivity mainActivity)
    {
        this.mainActivity = mainActivity;
    }

    public override void OnClick(View widget)
    {
        Intent browserIntent = new Intent(Intent.ActionView, Uri.Parse("http://www.somelink.com/"));
        mainActivity.StartActivity(browserIntent);
    }

    public override void UpdateDrawState(TextPaint ds)
    {
        base.UpdateDrawState(ds);
        ds.Color = Color.Red;
        ds.UnderlineText = false;
    }
}

什么时候使用:

TextView textview = FindViewById<TextView>(Resource.Id.Terms);

//You need add this line
textview.MovementMethod = LinkMovementMethod.Instance;

//This is the Text of TextView
SpannableString ss1 = new SpannableString("You must agree to our terms and conditions");

//22 means start position, ss1.Length() means end position
ss1.SetSpan(new MyClickableSpan(this), 22, ss1.Length(), SpanTypes.ExclusiveExclusive);

//set text for your TextView
textview.TextFormatted = ss1;

这篇关于Xamarin:无法从带有链接的 TextView 中检索 URLSpans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆