Android TextView文字链接4中步骤

Android TextView文字链接4中方法
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());这样的代码,否则无效。
Android TextView文字链接4中步骤

java代码
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.csdn.net/?232885我的CSDN博客 ";
        textView01.setText(autoLinkText);
        
        TextView textView02 = (TextView) findViewById(R.id.textView02);
        //String aLinkText = "<a href=\"http://student.csdn.net/?232885\">我的CSDN博客 </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.csdn.net/?232885\"><u>我的CSDN博客 </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文件

<?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.csdn.net/?232885">我的CSDN博客 </a>
    </string>
</resources>

main.xml文件
<?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"
    android:autoLink="all"
    >
	<TextView
	android:id="@+id/textView01"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    />
	<TextView
	android:id="@+id/textView02"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="@string/aLinkText"
    />
    <TextView
	android:id="@+id/textView03"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />
    <TextView
	android:id="@+id/textView04"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>