在android中制作一个超链接textview

发布于 2025-01-05 22:52:12 字数 119 浏览 1 评论 0原文

我想为文本视图文本创建链接,例如 Google。无论如何,有没有像这样的链接。 (即)当点击“Google”一词时,它应该打开相应的链接。欢迎任何想法。

I want to make a link for a textview text like Google. Is there anyway to make link like this. (i.e) When clicking on the word Google it should open the appropriate link. Any ideas are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧街凉风 2025-01-12 22:52:12

试试这个,让我知道会发生什么..

使用 java 代码:

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

从 API 级别 >= 24 开始 Html.fromHtml(String source) 已弃用,而是使用 fromHtml(String, int)

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

或者在布局 xml 文件中,在 TextView 小部件属性内

android:autoLink="web"
android:linksClickable="true"

Try this, and let me know what happen..

Using java code:

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

From API level >= 24 onwards Html.fromHtml(String source) is deprecated instead use fromHtml(String, int),

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

Or in layout xml file, inside your TextView widget attributes

android:autoLink="web"
android:linksClickable="true"
痴情换悲伤 2025-01-12 22:52:12

在 TextView 的 xml 中使用 android:autoLink="web" 。它应该自动转换可点击的网址(如果在文本中找到)

use android:autoLink="web" in your TextView's xml. It should automatically convert urls click-able (if found in text)

冬天旳寂寞 2025-01-12 22:52:12

全部经过测试且 100% 正常工作
解决方案:android:autoLink="web"
下面是一个完整的例子

示例布局 Xml

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="email"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/lostpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="web"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/defaultpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

string.xml 中的字符串

<string name="lostpassword">If you lost your password please contact <a href="mailto:[email protected]?Subject=Lost%20Password" target="_top">[email protected]</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>

All tested and working 100%
Solution: android:autoLink="web"
below is a complete example

Sample Layout Xml

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="email"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/lostpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="web"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/defaultpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

String in string.xml

<string name="lostpassword">If you lost your password please contact <a href="mailto:[email protected]?Subject=Lost%20Password" target="_top">[email protected]</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>
习惯成性 2025-01-12 22:52:12

这也可以通过使用 Textview 的默认属性来完成

android:autoLink="email"

This can also be done by using the default property of Textview

android:autoLink="email"
写下不归期 2025-01-12 22:52:12

注意:- Html.fromHtml 在 Android N 中已弃用

您需要检查并支持 Android N 和更高版本的 Android

                  //Set clickable true
                 tagHeading.setClickable(true);
                 
                  //Handlle click event
                  tagHeading.setMovementMethod(LinkMovementMethod.getInstance());

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>", Html.FROM_HTML_MODE_LEGACY));
                } else {
                    tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>"));
                }
 

或者

您可以不这样做不想以编程方式在 TextView 上添加自动链接标志。

android:autoLink="web"

android:linksClickable="true"

这样就不需要添加 标签。

这是一个缺点,如果您想在文本上添加超链接,则不能这样做。例如,你不能做这样的事情:- [hiteshsahu][1]

           <TextView
                android:id="@+id/tag_info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tag_ll"
                android:layout_gravity="left"
                android:layout_margin="10dp"
                android:autoLink="web"
                android:linksClickable="true"
                android:text="https://github.com/hiteshsahu"
                android:textColor="@color/secondary_text" />

两种方法的结果:-

https://github.com/hiteshsahu

Note :- Html.fromHtml is deprecated in Android N

You need to do check and support Android N and higher versions of Android

                  //Set clickable true
                 tagHeading.setClickable(true);
                 
                  //Handlle click event
                  tagHeading.setMovementMethod(LinkMovementMethod.getInstance());

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>", Html.FROM_HTML_MODE_LEGACY));
                } else {
                    tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>"));
                }
 

Alternatively

You can don't want to id programmatically add autoLink flag on TextView.

android:autoLink="web"

android:linksClickable="true"

This way You don't need to add <a href='somelink'> tags.

Which is a disadvantage, if you want to add hyperlink on a text you can't do it this way. eg you can't do something like this:- [hiteshsahu][1]

           <TextView
                android:id="@+id/tag_info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tag_ll"
                android:layout_gravity="left"
                android:layout_margin="10dp"
                android:autoLink="web"
                android:linksClickable="true"
                android:text="https://github.com/hiteshsahu"
                android:textColor="@color/secondary_text" />

The result from both approach:-

https://github.com/hiteshsahu

蓝天白云 2025-01-12 22:52:12

对于最新版本的 SDK,fromHtml 已弃用
使用下划线

String yourtext = "<a style='text-decoration:underline' href='http://www.sample.com'> Sample Website </a>";
    if (Build.VERSION.SDK_INT >= 24) {
        textView.setText(Html.fromHtml(yourtext, Html.FROM_HTML_MODE_LEGACY));
    } else {
        textView.setText(Html.fromHtml(yourtext));
    }

For Latest version of SDK fromHtml is deprecated
Use below line

String yourtext = "<a style='text-decoration:underline' href='http://www.sample.com'> Sample Website </a>";
    if (Build.VERSION.SDK_INT >= 24) {
        textView.setText(Html.fromHtml(yourtext, Html.FROM_HTML_MODE_LEGACY));
    } else {
        textView.setText(Html.fromHtml(yourtext));
    }
眼泪都笑了 2025-01-12 22:52:12

我制作了以下文本视图的扩展功能。

@SuppressLint("SetTextI18n")
fun TextView.makeHyperLink(url: String) {
    text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml("<a href='${url}'>${text}</a>", Html.FROM_HTML_MODE_COMPACT)
    } else {
        Html.fromHtml("<a href='${url}'>${text}</a>")
    }
    movementMethod = LinkMovementMethod.getInstance()
    isClickable = true
}

并像这样调用它

tvTos.makeHyperLink(tos_url)

您还可以传递文本作为参数。

I have made a following extension function of a textview.

@SuppressLint("SetTextI18n")
fun TextView.makeHyperLink(url: String) {
    text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml("<a href='${url}'>${text}</a>", Html.FROM_HTML_MODE_COMPACT)
    } else {
        Html.fromHtml("<a href='${url}'>${text}</a>")
    }
    movementMethod = LinkMovementMethod.getInstance()
    isClickable = true
}

and calling it like this

tvTos.makeHyperLink(tos_url)

You can also pass text as a parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文