Android TextView實(shí)現(xiàn)帶鏈接文字事件監(jiān)聽的三種常用方式示例
本文實(shí)例講述了Android TextView實(shí)現(xiàn)帶鏈接文字事件監(jiān)聽的三種常用方式。分享給大家供大家參考,具體如下:
/**
* TextView實(shí)現(xiàn)文字鏈接跳轉(zhuǎn)功能
* @description:
* @author ldm
* @date 2016-4-21 下午4:34:05
*/
public class TextViewLinkAct extends Activity {
private TextView tv_3;
private TextView tv_4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.link);
setTextViewLink();
}
/**
* 通過不同方式實(shí)現(xiàn)TextView中文字點(diǎn)擊鏈接跳轉(zhuǎn)功能
*
* @description:
* @author ldm
* @date 2016-4-21 下午4:24:13
*/
private void setTextViewLink() {
// 以Html格式href鏈接方式實(shí)現(xiàn)跳轉(zhuǎn)
tv_3 = (TextView) findViewById(R.id.text3);
tv_3.setText(Html
.fromHtml("<b>text3: Constructed from HTML programmatically.</b> Text with a "
+ "<a href=\"http://www.google.com\">link</a> "
+ "created in the Java source code using HTML."));
tv_3.setMovementMethod(LinkMovementMethod.getInstance());
// 通過SpannableString的setMovementMethod方法實(shí)現(xiàn)鏈接效果
SpannableString ss = new SpannableString(
"text4: Manually created spans. Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 30,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 31 + 6, 31 + 10,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_4 = (TextView) findViewById(R.id.text4);
tv_4.setText(ss);
tv_4.setMovementMethod(LinkMovementMethod.getInstance());
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:attr/listDivider"
android:orientation="vertical"
android:showDividers="middle" >
<!-- 通過在布局中autoLink屬性設(shè)置TextView的鏈接功能. -->
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:paddingBottom="8dp"
android:text="<b>text1: Various kinds
of data that will be auto-linked.</b> In
this text are some things that are actionable. For instance,
you can click on http://www.google.com and it will launch the
web browser. You can click on google.com too. If you
click on (415) 555-1212 it should dial the phone. Or just write
foobar@example.com for an e-mail link. If you have a URI like
http://www.example.com/lala/foobar@example.com you should get the
full link not the e-mail address. Or you can put a location
like 1600 Amphitheatre Parkway, Mountain View, CA 94043. To summarize:
https://www.google.com, or 650-253-0000, somebody@example.com,
or 9606 North MoPac Expressway, Suite 400, Austin, TX 78759."
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/text4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
其中通過在而已代碼中android:autoLink屬性的選項(xiàng)目有:none(無鏈接效果),web(網(wǎng)頁鏈接),email(發(fā)郵件),phone(打電話),map(定位)及all(默認(rèn)全都自動(dòng)鏈接)。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android ScrollView 下嵌套 ListView 或 GridView出現(xiàn)問題解決辦法
這篇文章主要介紹了ScrollView 下嵌套 ListView 或 GridView 會(huì)發(fā)列表現(xiàn)數(shù)據(jù)只能顯示一行。因?yàn)樗麄兌际菨L動(dòng)結(jié)構(gòu),兩個(gè)滾動(dòng)條放到一起就會(huì)引起沖突,這里提供解決辦法相關(guān)資料,需要的朋友可以參考下2017-07-07
Android中FlowLayout組件實(shí)現(xiàn)瀑布流效果
大家好,本篇文章主要講的是Android中FlowLayout組件實(shí)現(xiàn)瀑布流效果,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
BottomNavigationView?ViewPager2?Fragment底部菜單導(dǎo)航欄
這篇文章主要為大家介紹了BottomNavigationView?ViewPager2?Fragment底部菜單導(dǎo)航欄實(shí)現(xiàn)效果詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn)
這篇文章主要介紹了Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
Android開發(fā)歡迎頁點(diǎn)擊跳過倒計(jì)時(shí)進(jìn)入主頁
沒點(diǎn)擊跳過自然進(jìn)入主頁,點(diǎn)擊跳過之后立即進(jìn)入主頁,這個(gè)功能怎么實(shí)現(xiàn)呢,本文通過實(shí)例代碼給大家介紹Android開發(fā)歡迎頁點(diǎn)擊跳過倒計(jì)時(shí)進(jìn)入主頁,感興趣的朋友一起看看吧2023-12-12
一文深入探討Android Activity啟動(dòng)模式
在 Android 應(yīng)用開發(fā)中,Activity 是用戶界面的核心組件,而 Activity 的啟動(dòng)模式則是決定應(yīng)用界面如何在任務(wù)棧中交互、管理以及呈現(xiàn)的關(guān)鍵因素,本文將深入探討 Android 中的 Activity 啟動(dòng)模式,詳細(xì)解釋每種模式的用途、適用場景2023-08-08
Android編程實(shí)現(xiàn)手機(jī)拍照的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)手機(jī)拍照的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)手機(jī)拍照的操作步驟與具體細(xì)節(jié),需要的朋友可以參考下2016-11-11

