Android development tips-click the link in the tag or TextView control to pop up the Activity (Custom Action)

Source: Internet
Author: User

In sections 5.2.1 and 5.2.2, the <a> label and special texts (such as URLs, phone numbers, and emails) automatically recognized by TextView are introduced. You can click these tags to trigger different actions. Although these click actions can meet most of your needs, if you want to execute any custom actions when you click a link, the content of this section is not readable.

Now let's use the method described in Section 5.2.1 to re-view the content of the Html. java file and find a method to process Html tags. For example:
For example, the endA method. This method is used to process </a> labels. We will find the following statement in this method.

Text. setSpan (newURLSpan (h. mHref), where, len, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
Text is the SpannableStringBuilder object. This object can modify the text content and set a text segment to a Span. In Android, Span indicates the effect of a text segment. For example, link format, image, text with background color, etc.

In the above Code, use the setSpan method to set the text of a certain interval (specified by where and len) to URLSpan, that is, the link display effect. URLSpan indicates setting the text to the link effect. This class is a subclass of ClickableSpan. The URLSpan and ClickableSpan classes can be found in the android. text. style package. In fact, all Span classes are in the android. text. style package.
We can view the content of the URLSpan. java file by following the method of viewing the content of the Html. java file. We will see an onClick method. The Code is as follows:Copy codeThe Code is as follows: // override the onClick method in the ClickableSpan class. The onClick method is an abstract method in the ClickableSpan class.
@ Override
PublicvoidonClick (Viewwidget ){
Uriuri = Uri. parse (getURL ());
Contextcontext = widget. getContext ();
Intentintent = newIntent (Intent. ACTION_VIEW, uri );
Intent. putExtra (Browser. EXTRA_APPLICATION_ID, context. getPackageName ());
Context. startActivity (intent );
}

In The onClick method, obtain the URL set for the href attribute of the <a> tag, and call the corresponding Activity to display the webpage.

We can draw a conclusion from the source code of the onClick method and the name of the ClickableSpan class. In sections 5.2.1 and 5.2.2, the like phone number, Email, website address, and link are all described in the ClickableSpan onClick method and the corresponding Activity is called by Action to display different content. We can also use a similar method, that is, to implement the onClick method by ourselves, so that we can achieve the goal of custom click actions.

First, prepare two TextView controls. In this example, we use the SpannableString object to set the Span. The difference between SpannableString and SpannableStringBuilder is that SpannableString does not allow text modification, and only Span is allowed. SpannableStringBuilder allows text modification, you can also set Span.

The following code uses the method of implicitly creating a ClickableSpan object instance to set the Span, and overwrites the onClick method.Copy codeThe Code is as follows: packagemobile. android. ch05.link. activity;
Importandroid. app. Activity;
Importandroid. content. Intent;
Importandroid. OS. Bundle;
Importandroid. text. SpannableString;
Importandroid. text. Spanned;
Importandroid. text. method. LinkMovementMethod;
Importandroid. text. style. ClickableSpan;
Importandroid. view. View;
Importandroid. widget. TextView;
PublicclassMainextendsActivity
{
@ Override
PublicvoidonCreate (BundlesavedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextViewtextView1 = (TextView) findViewById (R. id. textview1 );
TextViewtextView2 = (TextView) findViewById (R. id. textview2 );
Stringtext1 = "show Activity1 ";
Stringtext2 = "show Activity2 ";
// Convert text into a SpannableString object
SpannableStringspannableString1 = newSpannableString (text1 );
SpannableStringspannableString2 = newSpannableString (text2 );
// Set all texts in text1 to ClickableSpan objects and implement the onClick Method
SpannableString1.setSpan (newClickableSpan ()
{
// In The onClick method, you can write the action to be executed when you click a link.
@ Override
PublicvoidonClick (Viewwidget)
{
Intentintent = newIntent (Main. this, Activity1.class );
// Display Activity1
StartActivity (intent );
}
}, 0, text1.length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// Set all texts in text2 to ClickableSpan objects and implement the onClick Method
SpannableString2.setSpan (newClickableSpan ()
{
// In The onClick method, you can write the action to be executed when you click a link.
@ Override
PublicvoidonClick (Viewwidget)
{
Intentintent = newIntent (Main. this, Activity2.class );
// Display Activity2
StartActivity (intent );
}
}, 0, text1.length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// Use the SpannableString object to set the content of two TextView controls
TextView1.setText (spannableString1 );
TextView2.setText (spannableString2 );
// When you click a link, you must set the MovementMethod object for any action to be executed.
TextView1.setMovementMethod (LinkMovementMethod. getInstance ());
TextView2.setMovementMethod (LinkMovementMethod. getInstance ());
}
}

Now let's take a look at the setSpan method. This method has four parameters. The first parameter requires a ClicableSpan object. The second and third parameters indicate the start position and the next character position of the text to be set to Span respectively, that is, start and end. The last parameter is a flag. In this example, it is set to Spanned. SPAN_EXCLUSIVE_EXCLUSIVE. This flag is of little significance in the TextView control, but in the EditText control, it indicates that the Span effect is not applied when the characters are input before and after the current Span effect. You can also set the following values.
1. Spanned. SPAN_EXCLUSIVE_INCLUSIVE: do not apply the Span effect to the characters entered before the Span, and apply the Span effect to the characters entered later.
2. Spanned. SPAN_INCLUSIVE_EXCLUSIVE: Apply the Span effect to the character entered before the Span, and do not apply the Span effect to the character entered later.
3. Spanned. SPAN_INCLUSIVE_INCLUSIVE: Apply the Span effect to all the characters entered before and after the Span.
In this example, the effect is 5.5. After you click the two links on the screen, the Activity1 and Activity2 interfaces are displayed respectively.

Related Article

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.