如何设置可点击和可选择的 TextView 文本? [英] How set TextView text both clickable and selectable?

查看:40
本文介绍了如何设置可点击和可选择的 TextView 文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 textview 加载包含链接的 html 文本(到网站,到电子邮件地址...)

My textview load html text that contains links (to website, to e-mail address ...)

tv = (TextView)((Activity)mContext).findViewById(R.id.entry_webview);
tv.setText(Html.fromHtml(myPage));

我设置tv.setMovementMethod(LinkMovementMethod.getInstance()); 使链接可点击.我设置 tv.setTextIsSelectable(true); 使文本可选.

I settv.setMovementMethod(LinkMovementMethod.getInstance()); to make the linke be clickable. I set tv.setTextIsSelectable(true); to make the text selectable.

会发生什么?TextView 只应用最后一个设置,在这种顺序的情况下,文本只能选择,链接不可点击,一个设置排除另一个.

What happens? TextView applies just the last setting, in this order case, text will be ONLY selectable and the link(s) won't be clickable, one setting excludes the other other one.

如果我在 XML 中的 TextView 中设置

If I set in TextView in XML

android:autoLink="all"
android:textIsSelectable="true"

链接不起作用(电子邮件是).

links don't work (e-mail yes).

有没有办法让文本既可点击又可选择?

Is there a way to make the text both clickable and selectable?

谢谢.

推荐答案

我遇到了同样的问题 - 这个解决方案对我有用:

I had the same issue - this solution works for me:

XML TextView 不应有任何链接/可选属性:

The XML TextView should not have any link/selectable attributes:

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

然后,按照以下顺序以编程方式设置所有内容:

Then, set everything programmatically respecting the following order:

textView.setText(Html.fromHtml(myHtml));
Linkify.addLinks(textView, Linkify.WEB_URLS);
textView.setTextIsSelectable(true); // API-11 and above
textView.setMovementMethod(LinkMovementMethod.getInstance());

这篇关于如何设置可点击和可选择的 TextView 文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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