TextView超链接不起作用?

时间:2012-07-10 12:31:05

标签: android xml android-layout hyperlink

为什么TextView超链接无效。

在自定义dialog box内使用超链接。

未显示超链接。

我错了。怎么解决它。给我指导。

XML代码

<TextView
android:id="@+id/google_Link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dip"
android:textSize="20dip"
android:linksClickable="true"  
android:autoLink="all"
android:textColorLink="#306EFF"
android:text="" />

Android代码

TextView googleLink = ( TextView ) layout.findViewById( R.id.google_Link );
googleLink.setClickable(true);
googleLink.setMovementMethod(LinkMovementMethod.getInstance());
googleLink.setText( Html.fromHtml( "<a href=`http://www.google.co.in`>Google</a>" ) );

Android Manifest Code

<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

提前感谢。

5 个答案:

答案 0 :(得分:5)

仅替换此链接,它将起作用:

        TextView textView=(TextView) findViewById(R.id.link);
        textView.setClickable(true);
        String linkTxt=getResources().getString(R.string.link);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(Html.fromHtml( linkTxt));

在strings.xml中添加:

<string name="link">&lt;a href=http://www.google.co.in&gt;Google&lt;/a&gt;</string>

答案 1 :(得分:0)

它无效,因为您无法将href设置为TextView

您需要设置一个OnClickListener,其中包含onClick方法:

String url = "http://www.google.co.in";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

之后,您可以将监听器设置为TextView,如下所示:googleLink.setOnClickListener(myListener);

然后再次运行应用程序,应正确处理点击。

答案 2 :(得分:0)

此问题的最佳解决方案是: 首先创建一个Textview。

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/link"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:text="@string/developed_by_bracecodes"/>

然后从strings.xml中将文本添加到Textview中,如下所示:

<string name="developed_by_bracecodes"><a href="http://www.bracecodes.com">Developed by Bracecodes</a>  </string>

N.B:不要忘记在链接

之前添加http://

然后在java代码中添加以下行:

TextView link = findViewById(R.id.link);
    link.setMovementMethod(LinkMovementMethod.getInstance());

快乐的编码! 谢谢!

答案 3 :(得分:0)

<TextView 
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:id="@+id/hyperlink" 
android:text="@string/hyperlink"
android:textColorLink="@color/quantum_yellow"
**android:autoLink="web"**
/>

使用autoLink=web自动将链接文本定义为可单击的超链接。您也可以使用textColorLink

来确定颜色

答案 4 :(得分:0)

您的解决方案在这里。 https://github.com/saket/Better-Link-Movement-Method

implementation 'me.saket:better-link-movement-method:1.1'



 message?.message?.let {
            chatMessageTextView.setText(HtmlCompat.fromHtml(it.trim(), HtmlCompat.FROM_HTML_MODE_LEGACY),TextView.BufferType.SPANNABLE)
        }

  chatMessageTextView.movementMethod = BetterLinkMovementMethod.newInstance().apply {
        setOnLinkClickListener { _, url ->
            // Handle click or return false to let the framework handle this link.

       
            handleMessageLink(itemView.context,url)
            true
        }
    }

     <TextView
            android:id="@+id/chat_message_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/files_layout"
            android:layout_marginStart="@dimen/space_ultra_small"
            android:text="@string/text_small"
            android:textColor="@color/black"
            android:padding="@dimen/space_ultra_small"
            android:textColorLink="@color/sky_blue"
            android:textSize="@dimen/text_size_large" />
相关问题