国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android TextView實現帶鏈接文字事件監聽的三種常用方式示例

2019-10-22 18:29:38
字體:
來源:轉載
供稿:網友

本文實例講述了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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳新县| 大兴区| 盘山县| 正宁县| 江孜县| 香港| 科技| 阿拉善盟| 枣庄市| 灌阳县| 宁海县| 环江| 剑河县| 大余县| 固镇县| 克拉玛依市| 金阳县| 靖江市| 扶绥县| 永仁县| 南雄市| 黄浦区| 绥滨县| 雷波县| 威远县| 张家口市| 延边| 新营市| 本溪| 安宁市| 赤壁市| 施秉县| 荣成市| 城口县| 柳江县| 枣庄市| 洛南县| 清丰县| 安达市| 长海县| 鄢陵县|