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="wrap_content">
<TextView
android:id="@+id/text4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Parcel p = Parcel.obtain();
String str = "text4: Click here to dial the phone.";
SpannableString ss = new SpannableString(str);
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置粗体
//连接
ss.setSpan(new C(p,this , 1), 13, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new C(p,this , 2), 19, 22,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new C(p,this , 3), 24, 28,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
}
}
C//重写URLSpan 访问本地
public class C extends URLSpan{
private Activity a;
private int flag ;
public C(Parcel src , Activity a , int flag ) {
super(src);
this.a = a ;
this.flag = flag;
}
public int describeContents() {
return 0;
}
public int getSpanTypeId() {
return 0;
}
public String getURL() {
return "";
}
public void writeToParcel(Parcel dest, int flags) {
}
public void onClick(View widget) {
System.out.println("-----------------------------"+flag);
}
}