Android TextView文字链接4中方法

本文详细介绍了Android中TextView中实现文字链接的多种方法,包括URL、电子邮件、电话号码等自动链接设置,通过资源文件引用、HTML格式化、Spannable类使用等方式,并提供了Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 的实现TextView中文字链接的方式有很多种。
总结起来大概有4种:
1.当文字中出现URL、E-mail、电话号码等的时候,可以将TextView的android:autoLink属性设置为相应的的值,如果是所有的类型都出来就是android:autoLink="all"。当然也可以在java代码里做, textView01.setAutoLinkMask(Linkify.ALL);
2.将要处理的文字写到一个资源文件,如string.xml,然后的java代码里引用(直接写在代码了是不可行的,会直接把文字都显示处理)
3.用Html类的fromHtml()方法格式化要放到TextView里的文字
4.用Spannable或实现它的类,如SpannableString来格式,部分字符串。

当然以上各种方法,不要在java代码里加上textView03.setMovementMethod(LinkMovementMethod.getInstance());这样的代码,否则无效。


java代码
Java代码 复制代码  收藏代码
  1. import android.app.Activity;   
  2. import android.graphics.Typeface;   
  3. import android.os.Bundle;   
  4. import android.text.Html;   
  5. import android.text.SpannableString;   
  6. import android.text.Spanned;   
  7. import android.text.method.LinkMovementMethod;   
  8. import android.text.method.ScrollingMovementMethod;   
  9. import android.text.style.StyleSpan;   
  10. import android.text.style.URLSpan;   
  11. import android.text.util.Linkify;   
  12. import android.widget.TextView;   
  13.   
  14. public class LinkTest extends Activity {   
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {   
  17.         super.onCreate(savedInstanceState);   
  18.         setContentView(R.layout.main);   
  19.            
  20.         TextView textView01 = (TextView) findViewById(R.id.textView01);   
  21.         textView01.setAutoLinkMask(Linkify.ALL);   
  22.         String autoLinkText = "http://student.youkuaiyun.com/?232885我的优快云博客 ";   
  23.         textView01.setText(autoLinkText);   
  24.            
  25.         TextView textView02 = (TextView) findViewById(R.id.textView02);   
  26.         //String aLinkText = "<a href=\"http://student.youkuaiyun.com/?232885\">我的优快云博客 </a>"    
  27.         //                  + "<a href=\"tel:4155551212\">and my phone number</a>";   
  28.         //textView02.setText(aLinkText);   
  29.         textView02.setMovementMethod(LinkMovementMethod.getInstance());   
  30.            
  31.         TextView textView03 = (TextView) findViewById(R.id.textView03);   
  32.            
  33.         String htmlLinkText = "<a href=\"http://student.youkuaiyun.com/?232885\"><u>我的优快云博客 </u></a>";   
  34.         textView03.setText(Html.fromHtml(htmlLinkText));   
  35.         textView03.setMovementMethod(LinkMovementMethod.getInstance());   
  36.            
  37.         TextView textView04 = (TextView) findViewById(R.id.textView04);   
  38.         SpannableString ss = new SpannableString("call: 4155551212.");   
  39.         ss.setSpan(new StyleSpan(Typeface.BOLD), 05, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   
  40.         ss.setSpan(new URLSpan("tel:4155551212"), 616, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   
  41.         textView04.setText(ss);   
  42.         textView04.setMovementMethod(LinkMovementMethod.getInstance());   
  43.            
  44.     }   
  45. }  
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.method.ScrollingMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.widget.TextView;

public class LinkTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView textView01 = (TextView) findViewById(R.id.textView01);
        textView01.setAutoLinkMask(Linkify.ALL);
        String autoLinkText = "http://student.youkuaiyun.com/?232885我的优快云博客 ";
        textView01.setText(autoLinkText);
        
        TextView textView02 = (TextView) findViewById(R.id.textView02);
        //String aLinkText = "<a href=\"http://student.youkuaiyun.com/?232885\">我的优快云博客 </a>" 
        //					+ "<a href=\"tel:4155551212\">and my phone number</a>";
        //textView02.setText(aLinkText);
        textView02.setMovementMethod(LinkMovementMethod.getInstance());
        
        TextView textView03 = (TextView) findViewById(R.id.textView03);
        
        String htmlLinkText = "<a href=\"http://student.youkuaiyun.com/?232885\"><u>我的优快云博客 </u></a>";
        textView03.setText(Html.fromHtml(htmlLinkText));
        textView03.setMovementMethod(LinkMovementMethod.getInstance());
        
        TextView textView04 = (TextView) findViewById(R.id.textView04);
        SpannableString ss = new SpannableString("call: 4155551212.");
        ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new URLSpan("tel:4155551212"), 6, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView04.setText(ss);
        textView04.setMovementMethod(LinkMovementMethod.getInstance());
        
    }
}


string.xml文件

Ruby代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <resources>   
  3.     <string name="hello">Hello World, LinkTest!</string>   
  4.     <string name="app_name">LinkTest</string>   
  5.     <string name="aLinkText">   
  6.         <a href="http://student.youkuaiyun.com/?232885">我的优快云博客 </a>   
  7.     </string>   
  8. </resources>  
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, LinkTest!</string>
    <string name="app_name">LinkTest</string>
    <string name="aLinkText">
    	<a href="http://student.youkuaiyun.com/?232885">我的优快云博客 </a>
    </string>
</resources>

main.xml文件
Ruby代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:autoLink="all"  
  7.     >   
  8.     <TextView   
  9.     android:id="@+id/textView01"  
  10.     android:layout_width="wrap_content"    
  11.     android:layout_height="wrap_content"  
  12.     />   
  13.     <TextView   
  14.     android:id="@+id/textView02"  
  15.     android:layout_width="wrap_content"    
  16.     android:layout_height="wrap_content"  
  17.     android:text="@string/aLinkText"  
  18.     />   
  19.     <TextView   
  20.     android:id="@+id/textView03"  
  21.     android:layout_width="wrap_content"    
  22.     android:layout_height="wrap_content"    
  23.     />   
  24.     <TextView   
  25.     android:id="@+id/textView04"  
  26.     android:layout_width="wrap_content"    
  27.     android:layout_height="wrap_content"    
  28.     />   
  29. </LinearLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值