在android中创建超链接textview

Sri*_*vas 68 url android hyperlink textview

我想为像Google这样的textview文本建立链接.反正有没有这样的链接.(即)当点击Google这个词时,它应该打开相应的链接.欢迎任何想法.

use*_*305 117

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

使用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));
Run Code Online (Sandbox Code Playgroud)

从API级别> = 24以后Html.fromHtml(String source)不推荐使用fromHtml(String, int),

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
Run Code Online (Sandbox Code Playgroud)

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

android:autoLink="web"
android:linksClickable="true"
Run Code Online (Sandbox Code Playgroud)

  • 它也可以通过使用android:autoLink ="email"来完成 (4认同)
  • 如果 strings.xml 中存在超链接文本,则所有建议的答案都不起作用。它的工作方式非常奇怪。如果我直接在布局 xml 中设置文本,它可以工作,但是当我在 java 代码中设置 textview 数据时,它不起作用。 (2认同)
  • 只是提一下,如果您使用 Java 代码,则需要从 XML 中删除 `android:autoLink="web"`。我两者都有,但没有用。 (2认同)
  • @Velu,您必须用 `&lt;![CDATA[string]&gt;&gt;` 包围字符串,其中 string 是带有 ulr 的整个字符串文本。花了一个小时试图解决这个问题...... (2认同)

waq*_*lam 51

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

  • 还包括android:linksClickable ="true" (14认同)
  • @Garg's 这是您在 textview 上设置的可能包含超链接的文本 (2认同)

Sha*_*waj 27

所有测试和工作100%
解决方案:android:autoLink="web"
下面是一个完整的示例

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" />
Run Code Online (Sandbox Code Playgroud)

string.xml中的字符串

<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</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>
Run Code Online (Sandbox Code Playgroud)

  • 如果我更改此行> http://www.cleverfinger.com.au/user-guide/ </a> </ string>以获取不同的文字,则无效:点击此处 (3认同)
  • 这仅适用于文本包含完整URL,如:www.something.com (2认同)

Ris*_*waj 6

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

android:autoLink="email"
Run Code Online (Sandbox Code Playgroud)


Hit*_*ahu 5

注意:-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>"));
                }
Run Code Online (Sandbox Code Playgroud)

或者

您可能不想通过id在TextView上添加autoLink标志。

android:autoLink =“ web”

android:linksClickable =“ true”

这样,您无需添加<a href='somelink'>标签。

这是一个缺点,如果您想添加hyperlink一个text,则无法采用这种方式。例如,你不能做这样的事情:-[ 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" />
Run Code Online (Sandbox Code Playgroud)

两种方法的结果:

https://github.com/hiteshsahu