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

 原文链接: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值