Yen*_*Tay 21 android hyperlink textview
我正在使用包含两个超链接的字符串实现TextView,如下所示,但链接没有打开新的浏览器窗口:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textSize="14sp"
android:clickable="true"
android:linksClickable="true"
android:textColorLink="@color/colorPrimary"
android:autoLink="web"
android:text="@string/agree_terms_privacy"/>
Run Code Online (Sandbox Code Playgroud)
在string.xml中
<string name="agree_terms_privacy">By continuing, you agree to our <a href="http://link1/terms">Terms of Use</a> and read the <a href="http://link1/privacy">Privacy Policy</a></string>
Run Code Online (Sandbox Code Playgroud)
nym*_*ria 25
在查看了多个Stack Overflow帖子后,这个解决方案对我有用.我根据您的实施量身定制了它:
1. 删除自动链接以支持使用LinkMovementMethod,并将linksClickable设置为true
<TextView
android:id="@+id/termsOfUse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:textSize="14sp"
android:clickable="true"
android:linksClickable="true"
android:textColorLink="@color/colorPrimary"
android:text="@string/agree_terms_privacy"/>
Run Code Online (Sandbox Code Playgroud)
如果您使用该android:autoLink="web"属性,则必须textView.setAutoLinkMask(0);在调用setText()TextView 之前覆盖它.您也可以将链接设置为在您的活动中可点击,如果您愿意,可以在Harshal的答案中设置,但我离开了它,因为您已经在布局中使用了它.我还在你的TextView中添加了一个id termsOfUse,我们将在稍后使用它.
2. 将<with <in strings.xml 替换为并删除网址周围的双引号
这是因为当您检索字符串资源时,它不会正确解析嵌入的html,并且由于某种原因它不会转义双引号.所以代替:
<string name="agree_terms_privacy">By continuing, you agree to our <a href="http://link1/terms">Terms of Use</a> and read the <a href="http://link1/privacy">Privacy Policy</a></string>
Run Code Online (Sandbox Code Playgroud)
你想做的事:
<string name="agree_terms_privacy">By continuing, you agree to our <a href=http://link1/terms>Terms of Use</a> and read the <a href=http://link1/privacy>Privacy Policy</a></string>
Run Code Online (Sandbox Code Playgroud)
3.解析字符串资源并将其绑定到TextView
Spanned policy = Html.fromHtml(getString(R.string.agree_terms_privacy));
TextView termsOfUse = (TextView)findViewById(R.id.termsOfUse);
termsOfUse.setText(policy);
termsOfUse.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)
注意: Html.fromHtml已在API 24中弃用(有关如何根据需要处理此问题的详细信息,请参阅此文章).我们使用此方法从字符串中获取预期的HTML格式.
Har*_*ake 10
看看下面的代码片段,希望它有所帮助,
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)
小智 7
这对我有用
在你的文本视图中添加这个:
android:autoLink="all"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)
https://www.youtube.com/watch?v=UnJxyfyDyHU
我希望这对你有帮助。
| 归档时间: |
|
| 查看次数: |
26599 次 |
| 最近记录: |