想实现用一个
TextView
中间某一小段文字做成超链接
,点击后能调用系统默认浏览器,跳转到指定网页这么一个功能。
其实如果需求不够复杂,比较简单通用的文字显示+超链接形式,那么此篇文章对你会有帮助。
1)首先在strings.xml
文件中直接写上TextView要显示的超链接文字,如下,超链接用html标签的写法来就行了
<string name="str_temp">我同意<a href="http://www.baidu.com/">用户协议</a>中的所有内容</string>
2)然后你得在布局中拥有一个这样的TextView,注意,不要再添加什么autoLink、linksClickable属性之类的,将strings.xml
定义的文本引入,也可以在java代码中引入。
<TextView
android:id="@+id/signup_txt_agreeTerms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_temp"/>
3)在你的Activity中,添加如下代码.一定要设置setMovementMethod()
方法
TextView textview = (TextView) findViewById(R.id.signup_txt_agreeTerms);
textview.setMovementMethod(LinkMovementMethod.getInstance()); //其实就这一句是关键
好了到此结束
网友评论