Add a Web link to the TextView in an activity and launch it into another activity!

Source: Internet
Author: User
Tags gettext

Can add a lot of properties, styles or what, the current function to complete is to click on the TextView inside a text link, into another activity inside!
For example, you can do the @XXX on Weibo, and click to enter his profile!

Here are the activity:

 Packagewq.gdky005;Importjava.util.ArrayList;ImportAndroid. R.color;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;Importandroid.text.Html;Importandroid.text.Spannable;ImportAndroid.text.SpannableStringBuilder;ImportAndroid.text.method.LinkMovementMethod;ImportAndroid.text.style.ClickableSpan;ImportAndroid.text.style.URLSpan;ImportAndroid.view.View;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classAbcactivityextendsActivity {PrivateTextView TV; Private StaticContext CTX; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); TV=NewTextView ( This); Tv.setclickable (false); CTX= This; String Htmllinktext= ""; Htmllinktext= "AAAAAAAAAA" + "<a style=\" color:red;\ "href=\" I am hyperlink ... \ ">@ Hyperlink Click event;</a>" + " <a style=\ "color:red;\" href=\ "I am hyperlink 2......\" >@ Hyperlink Click event;</a> "+" AAAAAAAAAAAAAAAA "; //the style of the text is overwritten and cannot be changed ...Tv.settext (html.fromhtml (Htmllinktext));        Tv.setmovementmethod (Linkmovementmethod.getinstance ()); Charsequence text=Tv.gettext (); if(Textinstanceofspannable) {            intEnd =text.length (); Spannable SP=(spannable) tv.gettext (); Urlspan[] URLs= Sp.getspans (0, end, Urlspan.class); Spannablestringbuilder style=NewSpannablestringbuilder (text); Style.clearspans ();//should clear old spans             for(Urlspan url:urls) {Myurlspan Myurlspan=NewMyurlspan (Url.geturl ()); Style.setspan (Myurlspan, Sp.getspanstart (URL), sp.getspanend (URL), Spannable.span_exclusive_inclusi            VE);        } tv.settext (style);    } setcontentview (TV); }    Private Static classMyurlspanextendsClickablespan {PrivateString Murl; Myurlspan (String url) {Murl=URL; } @Override Public voidOnClick (View widget) {toast.maketext (CTX, Murl, Toast.length_long). Show (); Widget.setbackgroundcolor (Color.parsecolor ("#00000000")); Intent Intent=NewIntent (Abcactivity.ctx, aaaactivity.class);                                Ctx.startactivity (Intent); }    }}

This is the color and style of the settings file.

TextView is used to display text, sometimes need to textview in the individual words set as hyperlinks, or set the color of individual words, fonts, etc., it is necessary to use the Spannable object, you can use the Spannable object to achieve the above settings Mytextview = (TextView) This. Findviewbyid (R.id.mytextview); //Create a Spannablestring objectSpannablestring SP =NewSpannablestring ("This sentence has Baidu hyperlinks, there are highlighting, so, or so, there are italic."); //Set up HyperlinksSp.setspan (NewUrlspan ("http://www.baidu.com"), 5, 7, spanned.span_exclusive_exclusive); //Set highlight style oneSp.setspan (NewBackgroundcolorspan (color.red), 17, 19, spannable.span_exclusive_exclusive); //Set highlight style twoSp.setspan (NewForegroundcolorspan (Color.yellow), 20,24, spannable.span_exclusive_inclusive); //Set ItalicSp.setspan (NewStylespan (Android.graphics.Typeface.BOLD_ITALIC), 27, 29, spannable.span_exclusive_inclusive); //spannablestring Object set to TextViewMytextview.settext (SP); //set TextView to clickMytextview.setmovementmethod (Linkmovementmethod.getinstance ());

Add a Web link to the TextView in an activity and launch it into another activity!

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.