Android TextView implements the encapsulation class of clicking a text hyperlink (without underline), androidtextview

Source: Internet
Author: User

Android TextView implements the encapsulation class of clicking a text hyperlink (without underline), androidtextview

In android, you sometimes need to set some hyperlinks on TextView and perform some operations when you click these hyperlinks. For example, some keywords on Sina Weibo will jump to the corresponding page when you click.


Let's look at the source code for implementation.

/***** Created by Mr. simple, Aug 21,201 41: 51: 40 PM. * Copyright (c) 2014, hehonghui@umeng.com All Rights Reserved. **###################################### ################ * # _ oo0oo _ # * # o8888888o # * #88 ". "88 # * # (|-_-|) # * #0 \ =/0 #*#___/'---'\___#*#. '\ | #'. # * #/\ |: | # * #/_ | -: -|-\ # * # | \-#/| # * # | \ _ | ''\ ---/'' | _/| # *#\. -\__'-'___/-. /#*#___'.. '/--. --\'.. '___#*#. "" '<'. ___ \_< |> _/___. '> '"". # * # | :'-\'.; '\_/';. '/-': | #*#\\'_. \___\/___/. -'// # * ##==== '-. ____'. ___\_____/___. -'___. -'= ==#* #' = --- = '#*#~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ####* # Buddha bless never has a BUG #*##*######################## ########################### */package com. umeng. community. utils; import android. content. context; import android. text. html; import android. text. spannable; import android. text. spannableStringBuilder; import android. text. textPaint; import android. text. method. linkMovementMethod; import android. text. style. clickableSpan; import android. text. style. URLSpan; import android. widget. textView;/*** @ author mrsimple */public abstract class TopicSpanWrapper extends ClickableSpan {protected Context mContext; protected TextView mTextView; /*** @ param context * @ param TV */public TopicSpanWrapper (Context context, TextView TV) {mContext = context; mTextView = TV; updateClickSpan (TV );} /*** update Span ** @ param rv */public void updateClickSpan (TextView TV) {// concatenate a hyperlink here. I added a ticket number after the previous step, you can remove String htmlLinkText = "<a href = 'Here you can enter a url'> #" + TV. getText (). toString () + "# </a>"; TV. setText (Html. fromHtml (htmlLinkText); TV. setMovementMethod (LinkMovementMethod. getInstance (); CharSequence text = TV. getText (); if (text instanceof Spannable) {int end = text. length (); Spannable sp = (Spannable) TV. getText (); URLSpan [] urls = sp. getSpans (0, end, URLSpan. class); SpannableStringBuilder style = new SpannableStringBuilder (text); style. clearSpans (); // shocould clear old spans for (URLSpan url: urls) {// set Span style. setSpan (this, sp. getSpanStart (url), sp. getSpanEnd (url), Spannable. SPAN_EXCLUSIVE_EXCLUSIVE);} TV. setText (style) ;}@ Override public void updateDrawState (TextPaint ds) {super. updateDrawState (ds); // remove the underline ds of the hyperlink. setUnderlineText (false );}}

Example:

Public class UserInfoActivity extends FragmentActivity implements OnClickListener {FragmentManager mFragmentMan; private TextView mUserNameTv;/** (non-Javadoc) * @ see android. app. activity # onCreate (android. OS. bundle) */@ Override protected void onCreate (Bundle savedInstanceState) {requestWindowFeature (Window. FEATURE_NO_TITLE); super. onCreate (savedInstanceState); setContentView (R. layout. response); initUIComponents ();}/***/private void initUIComponents () {mFragmentMan = getSupportFragmentManager (); mPostedFragment = new PostedFragment (); mFragmentMan. beginTransaction (). add (R. id. user_info_fragment_container, mPostedFragment, PostedFragment. class. getSimpleName ()). commit (); // the layout is not given. In short, user_name_ TV is a TextView mUserNameTv = (TextView) findViewById (R. id. user_name_ TV);/*** wrap the text in TextView into clickable hyperlinks */new TopicSpanWrapper (this, mUserNameTv) {@ Override public void onClick (View widget) {Toast. makeText (mContext, "click the hyperlink text", Toast. LENGTH_SHORT ). show ();}};}}

Effect:


After clicking




The Toast text in the image is inconsistent with the code because the text in the text is different from the text in my demo. Just ignore this problem.



How to achieve TextView, some text response click to jump to a new page

Adding a property in TextView: android: autoLink = "web" is equivalent to adding a link. I don't know if it is helpful to you.

How does android textview convert text to hyperlink through code?

Set the onclicklistener of textview. This listener calls the browser to open your link.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.