Android TextView实现带链接文字事件监听的三种常用方式示例
程序员文章站
2023-12-02 20:23:22
本文实例讲述了android textview实现带链接文字事件监听的三种常用方式。分享给大家供大家参考,具体如下:
/**
* textview实现文字链接...
本文实例讲述了android textview实现带链接文字事件监听的三种常用方式。分享给大家供大家参考,具体如下:
/** * textview实现文字链接跳转功能 * @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(); } /** * 通过不同方式实现textview中文字点击链接跳转功能 * * @description: * @author ldm * @date 2016-4-21 下午4:24:13 */ private void settextviewlink() { // 以html格式href链接方式实现跳转 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方法实现链接效果 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属性设置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属性的选项目有:none(无链接效果),web(网页链接),email(发邮件),phone(打电话),map(定位)及all(默认全都自动链接)。
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。