Android Development Tips Click the link pop-up activity (custom action) in a label or TextView control _android

Source: Internet
Author: User
Tags html tags
In sections 5.2.1 and 5.2.2, the <a> tags and TextView special text (URLs, phone numbers, emails, etc.) are introduced, which can be clicked to trigger different actions. Although these click Actions already meet most of the needs, the contents of this section must be read if the reader wants to perform any custom action when the link is clicked.

Now let's review the contents of the Html.java file using the method described in the 5.2.1 section, and find a way to handle HTML tags, for example
For example, the Enda method. This method is used to process </a> labels. We will find the following statement in the method.

Text.setspan (Newurlspan (h.mhref), where,len,spannable.span_exclusive_exclusive);
Where text is a Spannablestringbuilder object that can modify the text content and set a piece of text to span, in Android, span represents the effect of a piece of text, such as link form, image, text with background color, and so on.

The above code uses the Setspan method to set the text of an interval (the interval specified by the where and Len) to the Urlspan effect, which is the link display effect. Where Urlspan indicates that the text is set to a link effect, which is a subclass of Clickablespan, where 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.
When we view the contents of the Urlspan.java file by looking at the contents of the Html.java file, we see an OnClick method with the following code:
Copy Code code as follows:

Overrides 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, the URL of the <a> tag's href property setting is obtained, and the corresponding activity is invoked to display the Web page.

A conclusion can be drawn from the source code of the OnClick method and the name of the Clickablespan class. In sections 5.2.1 and 5.2.2, such as telephones, emails, URLs, and links are displayed in the Clickablespan class's OnClick method by invoking the corresponding activity in the action to display the different content. Then we can also adopt a similar approach, which is to implement the OnClick method ourselves, so that we can achieve the purpose of the custom click action.

Just do it, prepare two TextView controls first. In this example we use the Spannablestring object to set the difference between span,spannablestring and Spannablestringbuilder is that spannablestring does not allow text to be modified, only spans are allowed, Spannablestringbuilder allows you to modify the text, and also allows you to set span.

The following code sets a span by implicitly creating a Clickablespan object instance, and overrides the OnClick method in it.
Copy Code code 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 to Spannablestring object
Spannablestringspannablestring1=newspannablestring (Text1);
Spannablestringspannablestring2=newspannablestring (TEXT2);
Sets all the text in the Text1 to the Clickablespan object and implements the OnClick method
Spannablestring1.setspan (Newclickablespan ()
{
You can write actions to perform when you click a link in the OnClick method
@Override
Publicvoidonclick (Viewwidget)
{
Intentintent=newintent (Main.this,activity1.class);
Show Activity1
StartActivity (Intent);
}
},0,text1.length (), spanned.span_exclusive_exclusive);
Sets all the text in the Text2 to the Clickablespan object and implements the OnClick method
Spannablestring2.setspan (Newclickablespan ()
{
You can write actions to perform when you click a link in the OnClick method
@Override
Publicvoidonclick (Viewwidget)
{
Intentintent=newintent (Main.this,activity2.class);
Show Activity2
StartActivity (Intent);
}
},0,text1.length (), spanned.span_exclusive_exclusive);
To set the contents of two TextView controls using the Spannablestring object
Textview1.settext (SPANNABLESTRING1);
Textview2.settext (SPANNABLESTRING2);
When you click a link, you must set the Movementmethod object whenever there is an action to be performed
Textview1.setmovementmethod (Linkmovementmethod.getinstance ());
Textview2.setmovementmethod (Linkmovementmethod.getinstance ());
}
}

Now let's take a look at the Setspan method, which has 4 parameters. The first parameter needs to set a Clicablespan object, and the second and third arguments represent the position of the next character at the beginning and end of the text in the text to be set to span, that is, start and ends. The last parameter is a flag. Set to spanned.span_exclusive_exclusive in this example, the flag does not make much sense in the TextView control, but in the EditText control it represents the effect of the span in the front and back of the input word characters the current span effect. You can also set several similar values as follows.
1.spanned.span_exclusive_inclusive: Characters entered before span do not apply span effects, and the characters entered later apply span effects.
2.spanned.span_inclusive_exclusive: The characters entered before span apply the effect of span, and the characters entered later do not apply span effects.
3.spanned.span_inclusive_inclusive: The characters that are entered before and after spans apply the effect of span.
This example shows the effect as shown in Figure 5.5. When you click on 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.