textview Spannable 应用




import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class MyTextViewActivity extends Activity {
    private AlertDialog mAlertDialog;

    private class MyURLSpan extends URLSpan {

        public MyURLSpan(String url) {
            super(url);
        }

        @Override
        public void onClick(View widget) {
            // 调用super方法实现点击LinkText后跳转到指定网页
            super.onClick(widget);

            // 之所以要监听LinkText点击事件是为了点击LinkText后关闭Dlg
            if (mAlertDialog != null) {
                mAlertDialog.dismiss();
                mAlertDialog = null;
            }
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textview);
        // showDlgWithLinkText();

        TextView spannableTextView = (TextView) this.findViewById(R.id.tv);

        /**
         * 创建一个 SpannableString对象
         */
        SpannableString sp = new SpannableString("谷歌超链接、高亮显示、高亮1、高亮2、斜体、下划线.");

        /**
         * 设置超链接
         */
        sp.setSpan(new URLSpan("http://www.google.com"), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        /**
         * 设置高亮样式一
         */
        sp.setSpan(new BackgroundColorSpan(Color.RED), 11, 14, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        /**
         * 设置高亮样式二
         */
        sp.setSpan(new ForegroundColorSpan(Color.YELLOW), 15, 18,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        /**
         * 设置斜体
         */
        sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 19, 21,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        /**
         * 设置下划线
         */
        sp.setSpan(new UnderlineSpan(), 22, 25, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        /**
         * SpannableString对象设置给TextView
         */
        spannableTextView.setText(sp);

        /**
         * 设置TextView中的超链接可点击
         */
        spannableTextView.setMovementMethod(LinkMovementMethod.getInstance());

        TextView textView04 = (TextView) findViewById(R.id.tv2);
        SpannableString ss = new SpannableString("call: 4155551212.");
        ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new URLSpan("tel:4155551212"), 6, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView04.setText(ss);
        textView04.setMovementMethod(LinkMovementMethod.getInstance());
        
        TextView textView02 = (TextView) findViewById(R.id.tv3);
        textView02.setAutoLinkMask(Linkify.ALL);
        String aLinkText = "http://student.csdn.net/?232885我的CSDN博客"
                + "tel:4155551212and my phone number";
        textView02.setText(aLinkText);   
        textView02.setMovementMethod(LinkMovementMethod.getInstance());   
    }

    private void showDlgWithLinkText() {
        View dlgContentView = LayoutInflater.from(this).inflate(R.layout.dlg_with_link_text, null);

        // 设置字体加粗
        ((TextView) dlgContentView.findViewById(R.id.forward_to_ygc87_csdn_blog_title)).getPaint()
                .setFakeBoldText(true);

        TextView forwardToXiaoMiLogin = (TextView) dlgContentView.findViewById(R.id.link_text_view);

        // 要通过Html.fromHtml()方法转换带有LinkText的文本,然后将文本强转为Spannable类型
        Spannable textWithLinkText = (Spannable) Html
                .fromHtml(getString(R.string.forward_to_ygc87_csdn_blog));

        // 获取文本中原有的URLSpan类型的文本,保存起来
        int end = textWithLinkText.length();
        URLSpan[] urls = textWithLinkText.getSpans(0, end, URLSpan.class);

        // 使用textWithLinkText创建一个SpannableStringBuilder,通过clearSpans()方法清除原有的Span
        SpannableStringBuilder style = new SpannableStringBuilder(textWithLinkText);
        style.clearSpans();

        // 重新设置textWithLinkText中的URLSpan
        for (URLSpan url : urls) {
            MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
            style.setSpan(myURLSpan, textWithLinkText.getSpanStart(url),
                    textWithLinkText.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        // 将重新设置Span的文本设置为forwardToXiaoMiLogin的显示文字
        forwardToXiaoMiLogin.setText(style);

        // 通过setMovementMethod设置LinkMovementMethod类型来使LinkText有效
        forwardToXiaoMiLogin.setMovementMethod(LinkMovementMethod.getInstance());

        mAlertDialog = new AlertDialog.Builder(this).setView(dlgContentView)
                .setNegativeButton(R.string.confirm_know, null).show();
    }
}

你可能感兴趣的:(Android)