简体   繁体   中英

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? 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.

 android:autoLink="web"

I guess you want to handle span click event by yourself.
There two ways for this:
1)Create new class extends URLSpan
2)Create new class extends LinkMovementMethod

Please see below url, may be help you.
https://stackoverflow.com/a/16182500/596555

Add this to your tetView

android:text="@string/Documentation

Add this line to 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

TextView textView = findViewById(R.id.textView);      textView.setMovementMethod(LinkMovementMethod.getInstance());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM