Android 如何使文本视图中的链接可单击

Android 如何使文本视图中的链接可单击,android,hyperlink,textview,clickable,Android,Hyperlink,Textview,Clickable,我定义了以下TextView: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/txtCredits" android:autoLink="web" android:id="@+id/infoTxtCredits&q

我定义了以下TextView:

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/txtCredits"
    android:autoLink="web" android:id="@+id/infoTxtCredits"
    android:layout_centerInParent="true"
    android:linksClickable="true"></TextView>
但这确实:

<string name="txtCredits">www.google.com</string>
这很糟糕,因为我宁愿显示一个文本链接,也不愿显示完整的URL。

我只使用android:autoLink=web,而且效果很好。单击链接可打开浏览器并显示正确的页面


我能猜到的一件事是,链接上方还有其他视图。透明的内容会填充整个父对象,但不会在链接上方显示任何内容。在这种情况下,单击会转到此视图而不是链接。

出现问题的原因是它只会尝试匹配裸地址。比如www.google.com或者http://www.google.com.


把你的文字通读一遍就可以了。您必须以编程的方式进行,但它是有效的。

在API演示中,我找到了解决问题的方法:

文件Link.java:

我删除了TextView上的大多数属性,以匹配演示中的内容

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/txtCredits"/>
这就解决了问题。这是相当困难的发现和修复


重要提示:如果您正在调用setMovementMethod,请不要忘记删除autoLink=web。

这就是我如何通过代码解决TextView中可点击和可见链接的方法

private void setAsLink(TextView view, String url){
    Pattern pattern = Pattern.compile(url);
    Linkify.addLinks(view, pattern, "http://");
    view.setText(Html.fromHtml("<a href='http://" + url + "'>http://" + url + "</a>"));
}

例如,在正确格式化的Html链接上使用setMovementMethodLinkMovementMethod.getInstance和Html.fromHTML时,请确保不要使用setAutoLinkMaskLinkify.ALL。

上述解决方案对我不起作用,但以下解决方案起到了作用,而且看起来更干净一些。 首先,在字符串资源中,使用HTML实体编码定义标记开头的V形符号,即:

&lt;a href="http://www.google.com">Google&lt;/a>
而不是:

最后,在代码中,执行以下操作:

((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));

就这样。无需常规表达式或其他手动破解。

您只需在XML文本视图中添加以下内容:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"/>

如果要添加类似HTML的链接,只需执行以下操作:

添加类似HTML的资源字符串:

  <string name="link"><a href="https://www.google.pl/">Google</a></string>
就这样!是的,像autoLink和linksClickable这样的选项只在显式链接上工作,而不包装到HTML标记中,这对我来说也是非常误导的…

我注意到使用android:autoLink=web

它就像一个符咒。

用这个

TextView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.twitter.com/"));
                        startActivity(in);
                    }
                    
                });
并在清单文件中添加权限:

<uses-permission android:name="android.permission.INTERNET"/>

我不得不在几个地方找到它,但我最终得到了这个版本的代码

文件strings.xml:

这将创建两个带有文本链接text1和linktext2的可点击超链接,将用户重定向到谷歌。

您只需要以下内容:

android:autoLink="web"
将这一行插入到一个文本视图中,该文本视图可以通过对web的引用进行单击。URL被设置为此TextView的文本

例如:

 <TextView
    android:id="@+id/textViewWikiURL"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold"
    android:text="http://www.wikipedia.org/"
    android:autoLink="web" />

自动链接电话不适合我。下面这句话很有魅力

TextView tv = (TextView) findViewById(R.id.emergencynos);
String html2="<br><br>Fire - <b><a href=tel:997>997</a> </b></br></br>";
tv.append(Html.fromHtml(html2));
tv.setMovementMethod(LinkMovementMethod.getInstance());

如果使用基于XML的TextView,对于您的需求,您只需要做两件事:

在字符串中标识您的链接,例如这是我的网页。 您可以将其添加到XML内容或代码中

在包含TextView的XML内容中,添加以下内容:

 android:linksClickable="true"

 android:autoLink="web"

Richard,下次,您应该在布局XML的TextView下添加此代码

android:autoLink="all"
这应该是这样的

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/txtCredits"
    android:id="@+id/infoTxtCredits"
    android:autoLink="all"
    android:linksClickable="true">
</TextView>
我希望这能帮助你

字符串值=访问我的博客视图回调; TextView text=TextView findViewByIdR.id.text; text.setTextHtml.fromHtmlvalue; text.setMovementMethodLinkMovementMethod.getInstance;
在花了一段时间研究这个问题后,我发现:

android:autoLink=如果您的HTML中有完整链接,则web工作。以下内容将以蓝色突出显示并可单击: 一些文本 一些文本http://www.google.com view.setMovementMethodLinkMovementMethod.getInstance;将与以下内容一起工作,并将突出显示并可单击: 一些文本 一些文本http://www.google.com 一些文本 请注意,第三个选项有一个超链接,但标记之间的链接描述本身不是链接。android:autoLink=web不适用于此类链接

android:autoLink=web如果在XML中设置,将覆盖view.setMovementMethodLinkMovementMethod.getInstance;即。;第三类链接将突出显示,但不可单击。 这个故事的寓意是使用view.setMovementMethodLinkMovementMethod.getInstance;如果希望所有链接都可单击,请确保XML布局中没有android:autoLink=web。

我在文本视图中添加了这一行:android:autoLink=web 下面是在布局文件中使用的示例

layout.xml示例

我只是用了这个:

Linkify.addLinks(TextView, Linkify.ALL);
它使链接可点击,给定。

使用以下代码:

String html = "<a href=\"http://yourdomain.com\">Your Domain Name</a>"
TextView textview = (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setText(Html.fromHtml(html));
通过使用:

Linkify获取一段文本和一个正则表达式,并将文本中的所有正则表达式匹配项转换为可单击的链接:

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("http://example.com");
Linkify.addLinks(textView, Linkify.WEB_URLS);
别忘了

import android.widget.TextView;

将CDATA添加到字符串资源中

Strings.xml

<string name="txtCredits"><![CDATA[<a href=\"http://www.google.com\">Google</a>]]></string>

我的代码是这样的:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/link"
    android:text="@string/forgot"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:linksClickable="true"/>
/*TextView action*/
        TextView textView = (TextView) findViewById(R.id.link);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,forgot.class));
            }
        });  

这只是指向另一个活动的链接。但是这个链接是可以点击的,而且工作很顺利。在安卓Studio 1.5预览版中测试,公认的答案是正确的,但它将 ean表示电话号码、地图、电子邮件地址和常规链接,例如。,http://google.com 如果没有href,则标记将不再可单击,因为您无法在XML内容中拥有自动链接

我找到的所有东西都可以点击的唯一完整解决方案如下:

Spanned text = Html.fromHtml(myString);
URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
SpannableString buffer = new SpannableString(text);
Linkify.addLinks(buffer, Linkify.ALL);
for (URLSpan span : currentSpans) {
    int end = text.getSpanEnd(span);
    int start = text.getSpanStart(span);
    buffer.setSpan(span, start, end, 0);
}
textView.setText(buffer);
textView.setMovementMethod(LinkMovementMethod.getInstance());
TextView getapp =(TextView) findViewById(R.id.getapp);
getapp.setMovementMethod(LinkMovementMethod.getInstance());

TextView不应该有android:autolink。不需要安卓:linksClickable=true;默认情况下这是正确的。

这里有一行Android代码,可以让手机和URL从textView中选择,无论字符串是什么,数据是什么。您不需要为此使用任何HTML标记

TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("some URL is www.google.com phone 7504567890 another URL lkgndflg.com ");

// Makes the textView's Phone and URL (hyperlink) select and go.
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);

对我来说最简单的方法就是使用


它将自动从textview中的文本中检测web URL。

以下内容适用于在Android应用程序中寻找文本和超链接组合的任何人

在string.xml中:

现在,在活动或片段中,执行以下操作:

Spanned text = Html.fromHtml(myString);
URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
SpannableString buffer = new SpannableString(text);
Linkify.addLinks(buffer, Linkify.ALL);
for (URLSpan span : currentSpans) {
    int end = text.getSpanEnd(span);
    int start = text.getSpanStart(span);
    buffer.setSpan(span, start, end, 0);
}
textView.setText(buffer);
textView.setMovementMethod(LinkMovementMethod.getInstance());
TextView getapp =(TextView) findViewById(R.id.getapp);
getapp.setMovementMethod(LinkMovementMethod.getInstance());
到目前为止,您不需要使用这种方法设置android:autoLink=web或android:linksClickable=true。

[在棒棒糖前以及棒棒糖及更高版本中测试]

您可以从后端或资源文件获取HTML字符串。 如果将文本作为资源字符串,请确保添加CDATA标记:

还可以管理Linkify文本颜色


在SpannableString上创建扩展方法:

private fun SpannableString.setLinkSpan(text: String, url: String) {
    val textIndex = this.indexOf(text)
    setSpan(
        object : ClickableSpan() {
            override fun onClick(widget: View) {
                Intent(Intent.ACTION_VIEW).apply { data = Uri.parse(url) }.also { startActivity(it) }
            }
        },
        textIndex,
        textIndex + text.length,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
}
使用它使文本视图中的字符串可单击:

    myTextView.apply {
        movementMethod = LinkMovementMethod.getInstance()

        val googleUrl = "http://www.google.com"
        val microsoftUrl = "http://www.microsoft.com"

        val google = "Google"
        val microsoft = "Microsoft"

        val message = SpannableString("$google & $microsoft").apply {
            setLinkSpan(google, googleUrl)
            setLinkSpan(microsoft, microsoftUrl)
        }

        text = message
    }
享受吧



在上面添加了更多信息。这可能是我将字符串定义为Google的方式吗?看起来这是允许的,但对我不起作用。如果使用linkify auto,则不需要a href部分。操作系统将使用字符串解析url,并将每个url转换为可单击的链接。但这不会导致google这个词被链接到google.com。它会将www.google.com显示为link。使用autolink对我来说也很完美,没有家长的纯文本视图。安卓:autolink还可以很好地链接电话号码、地址和电子邮件地址或以上所有内容。不,不是,因为如果你不想使用一个锚定标签来显示与URL不同的文本,这个答案就不起作用了TextView中的链接与字符串中的其他文本颜色不同,所以我认为它们被识别为链接。它们只是不可点击:-这将在每次执行此代码时创建一个HTML解析器。如果您使用此功能在列表项内或应用程序中其他性能关键位置创建链接,请注意这一点。在创建listitem的过程中,这会将my getView方法的执行时间减少50%。此外,请注意,URL中的空格必须替换为%20。通过将字符串包装在标记中,可以避免实体编码问题。谢谢。不使用标签。这应该是答案!与我的原生应用程序配合得很好也许只有我一个人,但你为什么要用EditText来实现这一点?事实证明,实现这一目标只需要setMovementMethod。作为说明,我看到一些三星Galaxy S3的OS 4.3在使用setMovementMethodLinkMovementMethod.getInstance时崩溃,所以要考虑到这一点,在这里添加它的名称之前,我必须创建一个验证。请注意,如果调用setMovementMethod,则必须删除XML文件中的autolink=web,否则链接将无法工作。感谢您的评论,只需在您的xml中使用autoLink属性,例如:android:autoLink=web+1您是对的。应该有一个选项来输入自己的模式,以便匹配。这是需要添加到xml中的主线,它的工作方式很有魅力。谢谢@Richardwww.google.com使用上面的字符串,它就可以工作了。据悉,href和with-in-anchor标记中的字符串必须相同,且具有.com或。*若要工作,需要检查此处的某些文本谷歌查看上述代码片段,单击此处的某些文本在我的情况下不起作用,我该怎么办?尝试android:focusable=true解决了我的问题,但我希望看到文本,单击文本时,它将重定向到浏览器打开的链接?这个解决方案并不能解决这个问题,是吗?这是可行的,但请记住,除了这个可点击的文本视图之外,应用程序可能不需要互联网许可。也许在那些情况下,这真的与问题有关吗?这与问题无关。这永远不会失败!我喜欢这种方法,因为它允许我们只显示锚文本,而不是完整的URL。锚文本也可以设置样式,如绿色和下划线。不适合我。链接确实是可以点击的,但整个字符串都会显示出来,而不仅仅是谷歌。事实上,到目前为止,这里的答案或两者的组合都不起作用。您的设备或模拟器是否启用了internet,并将其设置为wifi,或是否已连接LAN,@Violet Giraffe?是的。我从来没有设法让链接只使用XML属性就可以点击,为此我不得不求助于Java代码。这没什么大不了的,但不好。这很有效。还要确保删除android:auto

链接=?从你的文本视图。这就是最终对我有用的东西。有效的HTML且未使用android:autoLink=?这很重要!对于Xamarin,单词显示但不带下划线,不能使用上述参数单击,而不是使用以下参数:autoLink、LinksClickalefor Xamarin设置这些参数并设置文本或设置文本格式化也会生成单词,但其不可单击,也不带蓝色/下划线。删除自动链接也会失败。仅删除链接可链接也会失败。删除两者也会失败。helloWorld.TextFormatted=Html.FromHtml链接URL;不管有没有这些,都是失败的。没有链接,没有蓝色,不能点击这个答案是最好的。适用于所有Android版本,我们只需要在文本视图中设置文本时添加该方法。奖励:它允许我们添加可定制的链接可点击真正最简单和最好的答案。它同时适用于电子邮件/网络。。。。谢谢你,伙计!!!如果您需要一些更新的高级功能,请使用LinkIfCompat。很好,它会生成没有链接的URL。deadsetMovementMethod不适用于您提到的第二个选项ie。。有什么更新吗?谢谢你指出自动链接不起作用。对于如此多的答案,这是唯一一个提到它的。非常感谢。对我来说,如果没有这个,这个被接受的答案很好@Kelly你能告诉我你遇到问题的android版本和设备吗?不适用于myapp://schemes深度链接。语法突出显示似乎关闭了。例如,接近文本长度。是吗?是的,但它们不会被点击。链接是可点击的。这很好,但当你从打开的web按back时,只关闭浏览器,而不返回应用程序。重要提示:如果你正在调用setMovementMethod,不要忘了删除autoLink=web。什么是autoLink phone?为什么是过去的意义?不再是这样了吗?这个代码可以用了。。。我只是在粘贴一个旧代码
Linkify.addLinks(TextView, Linkify.ALL);
String html = "<a href=\"http://yourdomain.com\">Your Domain Name</a>"
TextView textview = (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setText(Html.fromHtml(html));
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("http://example.com");
Linkify.addLinks(textView, Linkify.WEB_URLS);
import android.widget.TextView;
<string name="txtCredits"><![CDATA[<a href=\"http://www.google.com\">Google</a>]]></string>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/link"
    android:text="@string/forgot"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:linksClickable="true"/>
/*TextView action*/
        TextView textView = (TextView) findViewById(R.id.link);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,forgot.class));
            }
        });  
Spanned text = Html.fromHtml(myString);
URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
SpannableString buffer = new SpannableString(text);
Linkify.addLinks(buffer, Linkify.ALL);
for (URLSpan span : currentSpans) {
    int end = text.getSpanEnd(span);
    int start = text.getSpanStart(span);
    buffer.setSpan(span, start, end, 0);
}
textView.setText(buffer);
textView.setMovementMethod(LinkMovementMethod.getInstance());
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("some URL is www.google.com phone 7504567890 another URL lkgndflg.com ");

// Makes the textView's Phone and URL (hyperlink) select and go.
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
TextView txt_Message = (TextView) view.findViewById(R.id.txt_message);
txt_Message.setText("This is link https://www.google.co.in/");
Linkify.addLinks(txt_Message, Linkify.WEB_URLS);
<string name="applink">Looking for Digital Visiting card? 
<a href="https://play.google.com/store/apps/details?id=com.themarkwebs.govcard">Get it here</a>
</string>
<TextView
    android:id="@+id/getapp"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:textColor="@color/main_color_grey_600"
    android:textSize="15sp"
    android:text="@string/applink"/>
TextView getapp =(TextView) findViewById(R.id.getapp);
getapp.setMovementMethod(LinkMovementMethod.getInstance());
<string name="your_text">![CDATA[...<a href="your_link">Link Title</a>  ...]]</string>
String yourText = getString(R.string.your_text);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   textView.setText(Html.fromHtml(yourText, Html.FROM_HTML_MODE_COMPACT));
} else {
   textView.setText(Html.fromHtml(yourText));
}

try {
   subtext.setMovementMethod(LinkMovementMethod.getInstance());
} catch (Exception e) {
   //This code seems to crash in some Samsung devices.
   //You can handle this edge case base on your needs.
}
tv_customer_care_no.setLinkTextColor(getResources().getColor(R.color.blue));
tv_customer_care_no.setText("For us to reach out to you, please fill the details below or contact our customer care at 18004190899 or visit our website http://www.dupont.co.in/corporate-links/contact-dupont.html");
Linkify.addLinks(tv_customer_care_no, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
Linkify.addLinks(tv_customer_care_no, Linkify.ALL);
private fun SpannableString.setLinkSpan(text: String, url: String) {
    val textIndex = this.indexOf(text)
    setSpan(
        object : ClickableSpan() {
            override fun onClick(widget: View) {
                Intent(Intent.ACTION_VIEW).apply { data = Uri.parse(url) }.also { startActivity(it) }
            }
        },
        textIndex,
        textIndex + text.length,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
}
    myTextView.apply {
        movementMethod = LinkMovementMethod.getInstance()

        val googleUrl = "http://www.google.com"
        val microsoftUrl = "http://www.microsoft.com"

        val google = "Google"
        val microsoft = "Microsoft"

        val message = SpannableString("$google & $microsoft").apply {
            setLinkSpan(google, googleUrl)
            setLinkSpan(microsoft, microsoftUrl)
        }

        text = message
    }