一、簡介
也是TextView顯示文本控件兩種方法
也是顯示豐富的文本



二、方法
TextView兩種顯示link的方法
1)通過TextView里面的類html標(biāo)簽
* 1、設(shè)置好html標(biāo)簽的文本
String text1="<font color='red'><i>你好啊,陌生人</i></font><br/>";
text1+="<a href='http://www.baidu.com'>百度</a><br />";
* 2、為之前的文本聲明Html.fromHtml,方便TextView解析為html標(biāo)簽
tv_one.setText(Html.fromHtml(text1));
* 3、設(shè)置link點(diǎn)擊事件
tv_one.setMovementMethod(LinkMovementMethod.getInstance());
2)通過android:autoLink屬性
* 1、添加普通文本
String text2="我的網(wǎng)站:http://www.baidu.com /n";
text2+="我的電話:18883306749";
tv_two.setText(text2);
* 2、在layout的textView中設(shè)置android:autoLink屬性
android:autoLink="all"
三、代碼實(shí)例

點(diǎn)擊上面的百度和下面的百度鏈接。出現(xiàn)

點(diǎn)擊電話號碼。出現(xiàn)

代碼:
fry.Activity01
package fry;import com.example.textViewDemo1.R;import android.app.Activity;import android.os.Bundle;import android.text.Html;import android.text.method.LinkMovementMethod;import android.widget.TextView;public class Activity01 extends Activity{ private TextView tv_one; private TextView tv_two; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); tv_one=(TextView) findViewById(R.id.tv_one); tv_two=(TextView) findViewById(R.id.tv_two); /* * TextView兩種顯示link的方法 * 1)通過TextView里面的類html標(biāo)簽 * 1、設(shè)置好html標(biāo)簽的文本 * 2、為之前的文本聲明Html.fromHtml,方便TextView解析為html標(biāo)簽 * 3、設(shè)置link點(diǎn)擊事件 * * 2)通過android:autoLink屬性 * 1、添加普通文本 * 2、在layout的textView中設(shè)置android:autoLink屬性 * */ //通過TextView里面的類html標(biāo)簽來實(shí)現(xiàn)顯示效果 String text1="<font color='red'><i>你好啊,陌生人</i></font><br/>"; text1+="<a href='http://www.baidu.com'>百度</a><br />"; tv_one.setText(Html.fromHtml(text1)); //設(shè)置鼠標(biāo)移動事件,產(chǎn)生鏈接顯示,沒有這句話,進(jìn)不去百度 tv_one.setMovementMethod(LinkMovementMethod.getInstance()); //tv_two里面設(shè)置了android:autoLink="all",也就是自動顯示所有l(wèi)ink String text2="我的網(wǎng)站:http://www.baidu.com /n"; text2+="我的電話:18883306749"; tv_two.setText(text2); //因?yàn)槲以O(shè)置了android:autoLink屬性,故不需要下面這句也可以進(jìn)百度頁面,進(jìn)電話頁面 //tv_two.setMovementMethod(LinkMovementMethod.getInstance()); }}/textViewDemo1/res/layout/activity01.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_one" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="all" /></LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選