本文介绍常用的几种种让TextView中文本链接可点击的方法。先介绍Linkify方法,因为我发现它非常好用。
Linkify的官方中文介绍,第一句: http://developer.android.com/intl/zh-CN/reference/android/text/util/Linkify.html
Linkify take a piece of text and a regular expression and turns all of the regex matches in the text into clickable links.
可以看出,如果需要使用正则表达式匹配来使文本中的某段字符串可点击,那么使用Linkify再好不过。
我目前还想不到有哪种情况下面的代码中不能实现而只有Linkify可以实现的情况,但是我个人觉得Linkify比较方便。我已经使用的一种的情况就是有个TextView上有一段文本是一个网址,我要让它可点击,而且点击的时候需要带上其他参数,其实就是登录网站用的token。这时候就可以用正则匹配这段文本,然后构造一个Transform Filter来实现。
具体的使用我是看 http://dxs376263348.iteye.com/blog/455853 这个链接的,有兴趣的同学可以去看看。
当然,简单的情况还是推荐下面介绍的四种基本方法。
先附上代码,来自ApiDemo:
1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.text; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.graphics.Typeface; 23 import android.os.Bundle; 24 import android.text.Html; 25 import android.text.SpannableString; 26 import android.text.Spanned; 27 import android.text.method.LinkMovementMethod; 28 import android.text.style.StyleSpan; 29 import android.text.style.URLSpan; 30 import android.widget.TextView; 31 32 public class Link extends Activity { 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 setContentView(R.layout.link); 38 39 // text1 shows the android:autoLink property, which 40 // automatically linkifies things like URLs and phone numbers 41 // found in the text. No java code is needed to make this 42 // work. 43 44 // text2 has links specified by putting <a> tags in the string 45 // resource. By default these links will appear but not 46 // respond to user input. To make them active, you need to 47 // call setMovementMethod() on the TextView object. 48 49 TextView t2 = (TextView) findViewById(R.id.text2); 50 t2.setMovementMethod(LinkMovementMethod.getInstance()); 51 52 // text3 shows creating text with links from HTML in the Java 53 // code, rather than from a string resource. Note that for a 54 // fixed string, using a (localizable) resource as shown above 55 // is usually a better way to go; this example is intended to 56 // illustrate how you might display text that came from a 57 // dynamic source (eg, the network). 58 59 TextView t3 = (TextView) findViewById(R.id.text3); 60 t3.setText( 61 Html.fromHtml( 62 "<b>text3: Constructed from HTML programmatically.</b> Text with a " + 63 "<a href=\"http://www.google.com\">link</a> " + 64 "created in the Java source code using HTML.")); 65 t3.setMovementMethod(LinkMovementMethod.getInstance()); 66 67 // text4 illustrates constructing a styled string containing a 68 // link without using HTML at all. Again, for a fixed string 69 // you should probably be using a string resource, not a 70 // hardcoded value. 71 72 SpannableString ss = new SpannableString( 73 "text4: Manually created spans. Click here to dial the phone."); 74 75 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 30, 76 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 77 ss.setSpan(new URLSpan("tel:4155551212"), 31+6, 31+10, 78 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 79 80 TextView t4 = (TextView) findViewById(R.id.text4); 81 t4.setText(ss); 82 t4.setMovementMethod(LinkMovementMethod.getInstance()); 83 } 84 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- Copyright (C) 2007 The Android Open Source Project 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 --> 16 17 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content"> 20 21 <LinearLayout android:orientation="vertical" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:showDividers="middle" 25 android:divider="?android:attr/listDivider"> 26 27 <!-- Four TextView widgets, each one displaying text containing links. --> 28 29 <!-- text1 automatically linkifies things like URLs and phone numbers. --> 30 <TextView android:id="@+id/text1" 31 android:layout_width="match_parent" 32 android:layout_height="match_parent" 33 android:paddingBottom="8dp" 34 android:autoLink="all" 35 android:textAppearance="?android:attr/textAppearanceMedium" 36 android:text="@string/link_text_auto" 37 /> 38 39 <!-- text2 uses a string resource containing explicit <a> tags to 40 specify links. --> 41 <TextView android:id="@+id/text2" 42 android:layout_width="match_parent" 43 android:layout_height="match_parent" 44 android:paddingTop="8dp" 45 android:paddingBottom="8dp" 46 android:textAppearance="?android:attr/textAppearanceMedium" 47 android:text="@string/link_text_manual" 48 /> 49 50 <!-- text3 builds the text in the Java code using HTML. --> 51 <TextView android:id="@+id/text3" 52 android:layout_width="match_parent" 53 android:layout_height="match_parent" 54 android:paddingTop="8dp" 55 android:paddingBottom="8dp" 56 android:textAppearance="?android:attr/textAppearanceMedium" 57 /> 58 59 <!-- text4 builds the text in the Java code without using HTML. --> 60 <TextView android:id="@+id/text4" 61 android:layout_width="match_parent" 62 android:layout_height="match_parent" 63 android:paddingTop="8dp" 64 android:textAppearance="?android:attr/textAppearanceMedium" 65 /> 66 67 </LinearLayout> 68 </ScrollView>
1 <string name="link_text_auto"><b>text1: Various kinds 2 of data that will be auto-linked.</b> In 3 this text are some things that are actionable. For instance, 4 you can click on http://www.google.com and it will launch the 5 web browser. You can click on google.com too. If you 6 click on (415) 555-1212 it should dial the phone. Or just write 7 foobar@example.com for an e-mail link. If you have a URI like 8 http://www.example.com/lala/foobar@example.com you should get the 9 full link not the e-mail address. Or you can put a location 10 like 1600 Amphitheatre Parkway, Mountain View, CA 94043. To summarize: 11 https://www.google.com, or 650-253-0000, somebody@example.com, 12 or 9606 North MoPac Expressway, Suite 400, Austin, TX 78759. 13 </string> 14 <string name="link_text_manual"><b>text2: Explicit links using <a> markup.</b> 15 This has markup for a <a href="http://www.google.com">link</a> specified 16 via an <a> tag. Use a \"tel:\" URL 17 to <a href="tel:4155551212">dial a phone number</a>. 18 </string>
方法一:
给TextView加上
android:autoLink="all"
属性,这样,string中包含的网址、电话等都可以被点击。具体例子见代码中的text1。如果只想具体的元素可点击,可以设置其他值:
Constant | Value | Description |
none |
0x00 | Match no patterns (default). |
web |
0x01 | Match Web URLs. |
email |
0x02 | Match email addresses. |
phone |
0x04 | Match phone numbers. |
map |
0x08 | Match map addresses. |
all |
0x0f | Match all patterns (equivalent to web|email|phone|map). |
方法二:
给需要链接的字符串加上a标签,然后对TextView对象调用setMovementMethod()方法。具体例子见代码中的text2。
方法三和方法四是在代码中构造可点击的串,具体的方式可以参看代码,比较类似。使用的时候一定要记得防止hardcode!