LA_*_*LA_ 8 android hyperlink textview
我在TextView中有链接:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:linksClickable="true"
android:text="@string/app_description" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
该链接是资源的一部分:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="app_description">Some text with the <a href="http://www.example.com">link</a>.</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
不知何故,链接不起作用.可能是什么原因?它显示为链接.但是点击不会打开浏览器.
班级代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Run Code Online (Sandbox Code Playgroud)
shi*_*edi 10
你可以用这种方式使用http链接:
mLink = (TextView) findViewById(R.id.link);
if (mLink != null) {
mLink.setMovementMethod(LinkMovementMethod.getInstance());
}
Run Code Online (Sandbox Code Playgroud)
在XML中:
android:autoLink="all"
Run Code Online (Sandbox Code Playgroud)
重要的是,如果您正在使用:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml("<a href=https://www.google.com/>Click</a>", Html.FROM_HTML_MODE_LEGACY));
} else {
textView.setText(Html.fromHtml("<a href=https://www.google.com/>Click</a>"));
}
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)
不要 在 xml 中设置android:autoLink="all"或android:autoLink="web",因为它不起作用!
| 归档时间: |
|
| 查看次数: |
10073 次 |
| 最近记录: |