[英]android textview click on a link
If I have a textview with spanned text obtained from Html.fromHtml(source), how to make the textview to react when I click on a link?如果我有一个文本视图,其中包含从 Html.fromHtml(source) 获得的跨文本,如何使文本视图在我单击链接时做出反应? In my textview, link is something like "click here" and this text is a link, it's not a direct link like www.google.com.
在我的文本视图中,链接类似于“单击此处”,此文本是一个链接,而不是像 www.google.com 这样的直接链接。
android:autoLink="web"
I guess you want to handle span click event by yourself.我猜您想自己处理 span click 事件。
There two ways for this:有两种方法:
1)Create new class extends URLSpan 1)创建新类扩展 URLSpan
2)Create new class extends LinkMovementMethod 2)创建新类扩展LinkMovementMethod
Please see below url, may be help you.请参阅下面的网址,可能会对您有所帮助。
https://stackoverflow.com/a/16182500/596555 https://stackoverflow.com/a/16182500/596555
Butter to use Linkify黄油使用Linkify
Refer Android Text Links Using Linkify使用 Linkify 引用 Android 文本链接
Add this to your tetView将此添加到您的 tetView
android:text="@string/Documentation
Add this line to string.xml [ res/values]将此行添加到 string.xml [ res/values]
<string name="Documentation"><a href="https://developer.android.com/guide/components/activities/activity-lifecycle">Android Activity Lifecycle Documentation</a>
</string>
Add This code to your MainActivity.java将此代码添加到您的 MainActivity.java
TextView textView = findViewById(R.id.textView); textView.setMovementMethod(LinkMovementMethod.getInstance());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.