2016-08-23 24 views
-1

我想制作一个像“”这样的textView文本,同时检查类似产品“就像点击链接或网址一样(即)当有人点击时在文本上,它将命中一个Api来获取数据并显示一个新的活动。如何使android中的textView可点击或者像超链接一样

+3

关于'TextView'设置'OnClickListener'并做任何你想做的事情。 – Ironman

+0

尝试@Abhishek Patel答案并点击api ....运行异步..... –

+1

你可以使用'Html.fromhtml() '或使用'SpannableString' – Piyush

回答

1

为了让TextView的自动识别并将网址点击,你就必须把

机器人:自动链接= “网络” 在你的TextView的XML

财产。

2

试试这可以帮助你

tvLink.setText(Html.fromHtml("<a href=\"your url\">Meanwhile check the similar products</a>")); 
tvLink.setMovementMethod(LinkMovementMethod.getInstance()); 
+0

添加缺少的“\” –

+1

哦..感谢@Rotwang。 –

0
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)); 

,你也可以使用

android:autoLink="web" 
1

请使用如下代码

在Java文件

TextView txt=(TextView)findViewById(R.id.textView1); 
txt.setOnClickListener(new OnClickListener(){ 

public void onClick(View v){ 

//put Your API To call 

} 

}); 
+0

这是最简单的。只需将autoLink =“all”放入TextView xml即可。 – ShadowGod

+0

取决于你是如何做到这一点我想..在我的应用程序中我有一个“约”对话框,我在strings.xml中定义的字符串,我所要做的就是将自动链接和所有网址和电子邮件地址放入该字符串中获取自动链接 – ShadowGod

+0

例如,将autoLink =“all”添加到文本为“访问我的网站www.mywebsite.com并通过电子邮件发送给[email protected]”的TextView中 - 然后该网站和该电子邮件将会自动成为可点击的链接。 – ShadowGod

0

我在尝试很多情况,但总是失败。但终于找到了解决方案

如果你想用的TextView点击如下链接: enter image description here

您需要格式化你的TextView这样的:

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:id="@+id/textViewPrivacy" 
    android:gravity="center_horizontal" 
    android:text="@string/signUpPrivacyText" 
    android:linksClickable="true"/> 

注意,这android:linksClickable="true"是很重要的!

字符串资源必须是这样的:

<string name="signUpPrivacyText">By continuing you are indicating that you agree with <a href="http://example.com">Terms of Service</a> and <a href="http://example.com">Privacy Policy</a></string> 

然后,你需要这个在你的代码:

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

这一切!快乐编码!

相关问题