首页 > 解决方案 > 在 android 中使用 LinkMovementMethod 运行一个简单的链接

问题描述

如何使用以下命令运行简单链接?

<TextView
    android:text="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView1" />


TextView textView = FindViewById<TextView>(Resource.Id.textView1); 
textView.TextFormatted = Android.Text.Html.FromHtml("https://www.google.com"); 
textView.MovementMethod = Android.Text.Method.LinkMovementMethod.Instance;

这些工作:

   textView.TextFormatted = Android.Text.Html.FromHtml("<a href=\"" + "https://www.google.com" + "\">google</a>");
       textView.TextFormatted = Android.Text.Html.FromHtml("<a href=\"" + "https://www.google.com" + "\">https://www.google.com</a>");

这也不起作用:

<TextView
    android:text="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:id="@+id/textView1" />

 textView.TextFormatted = Android.Text.Html.FromHtml("<u>" + "https://www.google.com" + "</u>");

标签: hyperlinkxamarin.androidtextview

解决方案


LinkMovementMethod仅适用于文本中的链接。

在 Html<a>中代表一个 url 的超链接,但<u>不是超链接,它只是一个下划线。

避免使用可能与超链接混淆的元素。


推荐阅读