Android 在TextView中使用AutoLink,并自定义点击链接后的行为

来源:互联网 发布:龙华数据恢复 编辑:程序博客网 时间:2024/05/18 02:03
 原文链接:http://www.artinapp.com/blog/2012/10/30/android-%e5%9c%a8textview%e4%b8%ad%e4%bd%bf%e7%94%a8autolink%ef%bc%8c%e5%b9%b6%e8%87%aa%e5%ae%9a%e4%b9%89%e7%82%b9%e5%87%bb%e9%93%be%e6%8e%a5%e5%90%8e%e7%9a%84%e8%a1%8c%e4%b8%ba/
package com.carey.common.textautolink;import com.zyx.testb.R;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.text.Spannable;import android.text.SpannableStringBuilder;import android.text.style.ClickableSpan;import android.text.style.URLSpan;import android.text.util.Linkify;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class AutoLinkActivity extends Activity {    private TextView tvLinked = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_testautolink);        tvLinked = (TextView) findViewById(R.id.linkedText);        // set the TextView's link mask, auto link all.        tvLinked.setAutoLinkMask(Linkify.ALL);        // we have three links in this sentence        tvLinked.setText("http://www.baidu.com what is your website, 123456456 mine is www.hellokitty.com wish like it. thanks!!");        CharSequence content = tvLinked.getText();        SpannableStringBuilder builder = SpannableStringBuilder.valueOf(content);        // listen and get the spans        URLSpan[] spans = builder.getSpans(0, content.length(), URLSpan.class);        if (spans != null && spans.length > 0) {            int start = 0;            int end = 0;            for (URLSpan span : spans) {                start = builder.getSpanStart(span);                end = builder.getSpanEnd(span);                // to replace each link span with customized ClickableSpan                builder.removeSpan(span);                builder.setSpan(new MyURLSpan(AutoLinkActivity.this, span.getURL().toString()),                        start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);            }        }
<pre name="code" class="java"> // set the TextView's link mask, no auto link .
tvLinked.setAutoLinkMask(0); tvLinked.setText(builder); } class MyURLSpan extends ClickableSpan { private Context mContext = null; private String mUrl = ""; public MyURLSpan(Context context, String url) { this.mContext = context; this.mUrl = url; } @Override public void onClick(View widget) { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setTitle("Sure ?").setMessage("Confirm?") .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // do some customization, like open your own WebView // within your app. Toast.makeText(AutoLinkActivity.this, "you can do anything you like here", Toast.LENGTH_LONG).show(); } }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AutoLinkActivity.this, "you can do anything you like here", Toast.LENGTH_LONG).show(); } }).create().show(); } }}
activity_testautolink.xml
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/linkedText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="hello_world"        tools:context=".MainActivity" /></LinearLayout>


</pre><pre name="code" class="java">


0 0