TextView設定串連的方式

來源:互聯網
上載者:User

標籤:textview   連結   

1.設定TextView的autoLink屬性值
    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/text_main_webBaidu"             android:autoLink="web"/>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/text_main_telBaidu"             android:autoLink="phone"/>    </LinearLayout>

2.識別html代碼
   <TextView        android:id="@+id/tvHtml"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />
       <span style="white-space:pre"></span>// html識別連結tvHtml = (TextView) findViewById(R.id.tvHtml);String value = "<html><a href='http://www.baidu.com'>百度一下</a>(注意這裡的URL地址必須加http:協議)<br />"+ "連絡方式:<a href='tel://010-59928888'>010-59928888</a></html>";// 若開啟,與html識別起衝突// tvHtml.setAutoLinkMask(Linkify.ALL);tvHtml.setMovementMethod(LinkMovementMethod.getInstance());tvHtml.setText(Html.fromHtml(value));

3.SpannableStringBuilder手動設定串連

    <TextView        android:id="@+id/tvSpan"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />

        // span手動識別連結tvSpan = (TextView) findViewById(R.id.tvSpan);value = "[百度一下]\r\n客服電話:[010-59928888]";// 若開啟,仍起作用// tvSpan.setAutoLinkMask(Linkify.ALL);tvSpan.setMovementMethod(LinkMovementMethod.getInstance());SpannableStringBuilder builder = new SpannableStringBuilder(value);int indexStart = value.indexOf("[");int indexEnd = 0;while (indexStart != -1) {indexEnd = value.indexOf("]", indexStart);builder = builder.delete(indexEnd, indexEnd + 1);builder = builder.delete(indexStart, indexStart + 1);indexEnd--;value = builder.toString();final String temp = value.substring(indexStart, indexEnd);if (temp.equals("百度一下")) {builder.setSpan(new ClickableSpan() {@Overridepublic void onClick(View widget) {Toast.makeText(MainActivity.this, temp,Toast.LENGTH_SHORT).show();String url = "http://www.baidu.com";Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse(url));startActivity(intent);}}, indexStart, indexEnd, 0);}if (temp.equals("010-59928888")) {builder.setSpan(new ClickableSpan() {@Overridepublic void onClick(View widget) {String url = "tel://010-59928888";Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse(url));startActivity(intent);Toast.makeText(MainActivity.this, temp,Toast.LENGTH_SHORT).show();}}, indexStart, indexEnd, 0);}indexStart = value.indexOf("[", indexEnd);}tvSpan.setText(builder, BufferType.SPANNABLE);

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

TextView設定串連的方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.