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



二、方法
TextView兩種顯示link的方法
1)通過TextView里面的類html標(biāo)簽
* 1、設(shè)置好html標(biāo)簽的文本
String text1="<font color='red'><i>你好啊,陌生人</i></font><br/>";
text1+="<a >百度</a><br />";
* 2、為之前的文本聲明Html.fromHtml,方便TextView解析為html標(biāo)簽
tv_one.setText(Html.fromHtml(text1));
* 3、設(shè)置link點擊事件
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"
三、代碼實例

點擊上面的百度和下面的百度鏈接。出現(xià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點擊事件
*
* 2)通過android:autoLink屬性
* 1、添加普通文本
* 2、在layout的textView中設(shè)置android:autoLink屬性
*
*/
//通過TextView里面的類html標(biāo)簽來實現(xiàn)顯示效果
String text1="<font color='red'><i>你好啊,陌生人</i></font><br/>";
text1+="<a >百度</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);
//因為我設(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
為Android系統(tǒng)添加config.xml 新配置的設(shè)置
這篇文章主要介紹了為Android系統(tǒng)添加config.xml 新配置的設(shè)置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
item高度不同時Recyclerview獲取滑動距離的方法
這篇文章主要介紹了item高度不同時Recyclerview獲取滑動距離的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
Android 中 android.view.WindowLeaked的解決辦法
這篇文章主要介紹了Android 中 android.view.WindowLeaked的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05
WorkManager解決應(yīng)用退出后繼續(xù)運行后臺任務(wù)
這篇文章主要為大家介紹了WorkManager解決應(yīng)用退出后繼續(xù)運行后臺任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Android onMeasure與onDraw及自定義屬性使用示例
這篇文章主要介紹了Android onMeasure與onDraw及自定義屬性使用示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
Android 自定義View時使用TypedArray配置樣式屬性詳細(xì)介紹
這篇文章主要介紹了Android 自定義View時使用TypedArray配置樣式屬性詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11

