TextView兩種顯示link的方法,textview兩種link
TextView兩種顯示link的方法
一、簡介
也是TextView顯示文本控制項兩種方法
也是顯示豐富的文本
二、方法
TextView兩種顯示link的方法
1)通過TextView裡面的類html標籤
* 1、設定好html標籤的文本
String text1="<font color='red'><i>你好啊,陌生人</i></font><br/>";
text1+="<a href='http://www.baidu.com'>百度</a><br />";
* 2、為之前的文本聲明Html.fromHtml,方便TextView解析為html標籤
tv_one.setText(Html.fromHtml(text1));
* 3、設定link點擊事件
tv_one.setMovementMethod(LinkMovementMethod.getInstance());
2)通過android:autoLink屬性
* 1、添加普通文本
String text2="我的網站:http://www.baidu.com \n";
text2+="我的電話:18883306749";
tv_two.setText(text2);
* 2、在layout的textView中設定android:autoLink屬性
android:autoLink="all"
三、代碼執行個體
點擊上面的百度和下面的百度連結。出現
點擊電話號碼。出現
代碼:
fry.Activity01
1 package fry; 2 3 import com.example.textViewDemo1.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.text.Html; 8 import android.text.method.LinkMovementMethod; 9 import android.widget.TextView;10 11 public class Activity01 extends Activity{12 private TextView tv_one;13 private TextView tv_two;14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 // TODO Auto-generated method stub17 super.onCreate(savedInstanceState);18 setContentView(R.layout.activity01);19 20 tv_one=(TextView) findViewById(R.id.tv_one);21 tv_two=(TextView) findViewById(R.id.tv_two);22 23 /*24 * TextView兩種顯示link的方法25 * 1)通過TextView裡面的類html標籤26 * 1、設定好html標籤的文本27 * 2、為之前的文本聲明Html.fromHtml,方便TextView解析為html標籤28 * 3、設定link點擊事件29 * 30 * 2)通過android:autoLink屬性31 * 1、添加普通文本32 * 2、在layout的textView中設定android:autoLink屬性33 * 34 */35 36 //通過TextView裡面的類html標籤來實現顯示效果37 String text1="<font color='red'><i>你好啊,陌生人</i></font><br/>";38 text1+="<a href='http://www.baidu.com'>百度</a><br />";39 40 tv_one.setText(Html.fromHtml(text1));41 //設定滑鼠移動事件,產生連結顯示,沒有這句話,進不去百度42 tv_one.setMovementMethod(LinkMovementMethod.getInstance());43 44 //tv_two裡面設定了android:autoLink="all",也就是自動顯示所有link45 String text2="我的網站:http://www.baidu.com \n";46 text2+="我的電話:18883306749";47 tv_two.setText(text2);48 //因為我設定了android:autoLink屬性,故不需要下面這句也可以進百度頁面,進電話頁面49 //tv_two.setMovementMethod(LinkMovementMethod.getInstance());50 51 52 53 }54 }
/textViewDemo1/res/layout/activity01.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/tv_one" 9 android:layout_width="match_parent"10 android:layout_height="wrap_content" />11 12 <TextView13 android:id="@+id/tv_two"14 android:layout_width="match_parent"15 android:layout_height="wrap_content" 16 android:autoLink="all"17 />18 19 </LinearLayout>