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



二、方法
TextView兩種顯示link的方法
1)通過TextView里面的類html標簽
* 1、設置好html標簽的文本
String text1="<font color='red'><i>你好啊,陌生人</i></font><br/>";
text1+="<a >百度</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
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標簽
* 1、設置好html標簽的文本
* 2、為之前的文本聲明Html.fromHtml,方便TextView解析為html標簽
* 3、設置link點擊事件
*
* 2)通過android:autoLink屬性
* 1、添加普通文本
* 2、在layout的textView中設置android:autoLink屬性
*
*/
//通過TextView里面的類html標簽來實現顯示效果
String text1="<font color='red'><i>你好啊,陌生人</i></font><br/>";
text1+="<a >百度</a><br />";
tv_one.setText(Html.fromHtml(text1));
//設置鼠標移動事件,產生鏈接顯示,沒有這句話,進不去百度
tv_one.setMovementMethod(LinkMovementMethod.getInstance());
//tv_two里面設置了android:autoLink="all",也就是自動顯示所有l(wèi)ink
String text2="我的網站:http://www.baidu.com \n";
text2+="我的電話:18883306749";
tv_two.setText(text2);
//因為我設置了android:autoLink屬性,故不需要下面這句也可以進百度頁面,進電話頁面
//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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
為Android系統(tǒng)添加config.xml 新配置的設置
這篇文章主要介紹了為Android系統(tǒng)添加config.xml 新配置的設置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
item高度不同時Recyclerview獲取滑動距離的方法
這篇文章主要介紹了item高度不同時Recyclerview獲取滑動距離的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
Android 中 android.view.WindowLeaked的解決辦法
這篇文章主要介紹了Android 中 android.view.WindowLeaked的解決辦法的相關資料,需要的朋友可以參考下2017-05-05
WorkManager解決應用退出后繼續(xù)運行后臺任務
這篇文章主要為大家介紹了WorkManager解決應用退出后繼續(xù)運行后臺任務示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
Android onMeasure與onDraw及自定義屬性使用示例
這篇文章主要介紹了Android onMeasure與onDraw及自定義屬性使用示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02
Android 自定義View時使用TypedArray配置樣式屬性詳細介紹
這篇文章主要介紹了Android 自定義View時使用TypedArray配置樣式屬性詳細介紹的相關資料,需要的朋友可以參考下2016-11-11

