Android App---Use Linkify + regular to differentiate micro-blog text links and jump processing

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/ryan1012/archive/2011/07/12/2104087.html

Like Sina Weibo Android version of the app, when we click on the micro-blog text link will automatically jump to the interface, or the site link to the page to browse, or the mailbox link to jump mailbox service, or phone Number link jump dial interface. Android has helped us design a class: Linkify

Linkify is an auxiliary class that automatically creates hyperlinks in the TextView class (and inherited classes) by matching regex styles. Text that conforms to a particular regex style is turned into clickable hyperlinks that implicitly call StartActivity (The new Intent (Intent.action_view, URI)) and the text that conforms to the target URI.

You can specify any string style as a link, and the Linkify class provides pre-built generic content types (such as phone numbers and e-mail, Web addresses).

Java code:

TextView TextView = (TextView) Findviewbyid (R.id.mytextview);
Linkify.addlinks (TextView, linkify.web_urls| linkify.email_addresses);

You can use the Android:autolink feature in the layout resource to make a link to a view. It supports one or more custom values (with | split): None, Web, email, phone, or all. The next XML fragment shows how to add a hyperlink to a phone number and e-mail address:

<textview
Android:id= "@+id/txt"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
Android:textcolor= "#FFFFFF"
android:textcolorlink= "#0082CB"
android:autolink= "Web|phone|email" >
</TextView>

To create a custom link string:

In order to define your own link string, you need to create a regex style to match the text and then display it as a hyperlink. As with the local type, the target view is specified by calling Linkify.addlinks, but this time, the new regex style is passed in. You can also pass in a prefix, which is added to the target URI when the link is clicked.

int flags = pattern.case_insensitive;
Pattern p = pattern.compile ("\\bquake[0-9]*\\b", flags);
Linkify.addlinks (Mytextview, p, "content://com.paad.earthquake/earthquakes/");

About Linkify to this end, the implementation is now implemented using Linkify + regular-like microblogging link interface jump, step by step to do a demo.

The first step: Create a new Android project named Linktest;

The second step: Create a new strings.xml resource file under Res/values to store the micro-blog text data;

 <?xml version= 1.0   " Encoding="  utf-8   "? ><resources><string  name=  " hello   >" Official abuse of the reporter was reported after the exposure of the microblog was criticized "May 4, Xinjiang People's Radio @ Xinjiang news broadcast Sun Jianzhong in the Kashgar Prefecture Construction Bureau interview, the administrative office director Homin abusive , after being exposed by Micro Bo, Homin Day by the construction bureau to inform criticism, deduct one months wages and cancel the year-end appraising eligibility. Sun Jianzhong said he had received a Homin phone call to apologize to his face. Http:// T.CN/HG3PXV</STRING>  <string  name= app_name   >linktest</
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" > "Official abusive interview reporters were reported after the exposure of the microblog criticism" May 4, Xinjiang People's Radio @ Xinjiang news broadcast Sun Jianzhong in the construction bureau of Kashgar Prefecture, the administrative Office Director Homin abuse, after being exposed by Micro Bo, Homin Day by the construction bureau to inform criticism, deduct one months wages and cancel the year-end appraising eligibility. Sun Jianzhong said he had received a Homin phone call to apologize to his face. Http://t.cn/hg3pxv</string>
<string name= "App_name" >LinkTest</string>
</resources>

Step three: Modify the Main.xml layout file, where only one textview is added as the carrier of the microblog text;

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="fill_parent"Android:layout_height="fill_parent"><textview android:id="@+id/txt"Android:layout_width="fill_parent"Android:layout_height="wrap_content"android:text="@string/hello"Android:textcolor="#FFFFFF"android:textcolorlink="#0082CB"Android:autolink="Web|phone|email"/></linearlayout>

Fourth step: Define a public class Defs.java, which is used to store the custom link string;

="devdiv://sina_profile"="devdiv://sina_profile1" = " uid";}  

Fifth step: Define a class Mentionsactivity.java for the link jump interface mentioned in "@" on the microblog text;

Package Com.linktest;import Android.app.activity;import android.net.uri;import android.os.bundle;import Android.util.log;publicclass Mentionsactivity extends Activity {privatestaticfinal String TAG="mentionsactivity";p rivatestaticfinal Uri Profile_uri=Uri.parse (Defs.mentions_schema);PrivateString uid; @Overrideprotectedvoid onCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate); Extractuidfromuri (); Settitle ("Profile:hello,"+uid);} Privatevoid Extractuidfromuri () {URI Uri=getintent (). GetData ();if(URI! =NULL&&profile_uri.getscheme (). Equals (Uri.getscheme ())) {UID=Uri.getqueryparameter (DEFS.PARAM_UID); LOG.D (TAG,"uid from URL:"+uid); }}}

Sixth step: Define a class Trendsactivity.java, for the microblogging text on the "# #" topic Link Jump interface;

Package Com.linktest;import Android.app.activity;import android.net.uri;import android.os.bundle;import Android.util.log;publicclass Trendsactivity extends Activity {privatestaticfinal String TAG="trendsactivity";p rivatestaticfinal Uri Profile_uri=Uri.parse (Defs.trends_schema);PrivateString uid; @Overrideprotectedvoid onCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate); Extractuidfromuri (); Settitle ("Profile1:hello,"+uid);} Privatevoid Extractuidfromuri () {URI Uri=getintent (). GetData ();if(URI! =NULL&&profile_uri.getscheme (). Equals (Uri.getscheme ())) {UID=Uri.getqueryparameter (DEFS.PARAM_UID); LOG.D (TAG,"uid from URL:"+uid); }}}

The seventh step: Define the application instance of the main interface class Mainactivity.java, which uses the Linkify + regular to distinguish micro-blog text links, processing jump interface;

Package Com.linktest;import Java.util.regex.matcher;import java.util.regex.pattern;import android.app.Activity ; Import Android.os.bundle;import Android.text.util.linkify;import Android.text.util.linkify.matchfilter;import Android.text.util.linkify.transformfilter;import Android.util.log;import Android.widget.textview;publicclass Mainactivity extends Activity {privatestaticfinal String TAG="mainactivity"; @Overridepublicvoid onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); TextView txt=(TextView) Findviewbyid (r.id.txt); Extractmention2link (TXT);} Publicstaticvoid Extractmention2link (TextView v) {v.setautolinkmask (0); Pattern Mentionspattern= Pattern.compile ("@ (\\w+?) (? =\\w|$) (.)"); String Mentionsscheme= String.Format ("%s/?%s=", Defs.mentions_schema, Defs.param_uid); Linkify.addlinks (V, Mentionspattern, Mentionsscheme,NewMatchfilter () {@Overridepublicboolean acceptmatch (charsequence s),intStartintend) {returnS.charat (end-1) !='.';}}, NewTransformfilter () {@Override Publicstring Transformurl (Matcher match, string url) {log.d (TAG, Match.group (1));returnMatch.group (1); }}); Pattern Trendspattern= Pattern.compile ("# (\\w+?) #"); String Trendsscheme= String.Format ("%s/?%s=", Defs.trends_schema, Defs.param_uid); Linkify.addlinks (V, Trendspattern, Trendsscheme,NULL,NewTransformfilter () {@Override Publicstring Transformurl (Matcher match, string url) {log.d (TAG, Match.group (1));returnMatch.group (1); }});} }

Eighth Step: Modify Androidmanifest.xml, register all activity of the application instance.

<?xml version="1.0"encoding="Utf-8"? ><manifest xmlns:android="http://schemas.android.com/apk/res/android"Android:versioncode="1"Android:versionname="1.0"Package="com.linktest"><application android:icon="@drawable/icon"Android:label="@string/app_name"><activity android:name="mainactivity"Android:label="@string/app_name"><intent-filter><action android:name="Android.intent.action.MAIN"/><category android:name="Android.intent.category.LAUNCHER"/></intent-filter></activity><activity android:name="mentionsactivity"><intent-filter><action android:name="Android.intent.action.VIEW"/><category android:name="Android.intent.category.DEFAULT"/><category android:name="Android.intent.category.BROWSABLE"/><data android:scheme="DevDiv"android:host="Sina_profile"/></intent-filter></activity><activity android:name="trendsactivity"><intent-filter><action android:name="Android.intent.action.VIEW"/><category android:name="Android.intent.category.DEFAULT"/><category android:name="Android.intent.category.BROWSABLE"/><data android:scheme="DevDiv"android:host="Sina_profile1"/></intent-filter></activity></application></manifest>

Last step: Run the above Android project as shown in the effect:

To this end, the use of linkify + regular-type micro-blog text link and jump processing.

Android App---Use Linkify + regular to differentiate micro-blog text links and jump processing

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.