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)
waq*_*lam 51
使用android:autoLink="web"
在你的TextView中的XML.它应该自动转换网址可点击(如果在文本中找到)
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)
注意:-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)
两种方法的结果: