textview 单击事件




import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
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.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyTextViewActivity extends Activity {
    private TextView mClickableText;
    private TextView tv;
    public Context ctx;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        mClickableText = new TextView(this);
        mClickableText.setClickable(true);
        mClickableText.setTextSize(20);

        layout.addView(mClickableText);
        
        mClickableText.setText(getClickableSpan());
        mClickableText.setMovementMethod(LinkMovementMethod.getInstance());

        tv = new TextView(this);
        ctx = this;

        String htmlLinkText = "我是超链接" + "<a style=\"color:red;\" href='www.baidu.com'>超链接点击事件</a>";
        // 文字的样式(style)被覆盖,不能改变……

        tv.setText(Html.fromHtml(htmlLinkText));
        tv.setMovementMethod(LinkMovementMethod.getInstance());

        layout.addView(tv);

        CharSequence text = tv.getText();
        if (text instanceof Spannable) {
            int end = text.length();
            Spannable sp = (Spannable) tv.getText();
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.clearSpans();// should clear old spans

            // 循环把链接发过去
            for (URLSpan url : urls) {
                MyURLSpan myURLSpan = new MyURLSpan(url.getURL(), ctx);
                style.setSpan(myURLSpan, sp.getSpanStart(url),
                        sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            }
            tv.setText(style);
        }
        
        setContentView(layout);
    }

    private SpannableString getClickableSpan() {
        View.OnClickListener l = new View.OnClickListener() {
            // 如下定义自己的动作
            public void onClick(View v) {
                Toast.makeText(MyTextViewActivity.this, "Click Success", Toast.LENGTH_SHORT)
                        .show();

                // 在这里就可以做跳转到activity或者弹出对话框的操作了
            }
        };

        SpannableString spanableInfo = new SpannableString("This is a test, Click Here!");
        int start = 16;
        int end = spanableInfo.length();
        spanableInfo.setSpan(new Clickable(l), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanableInfo;
    }
}

class Clickable extends ClickableSpan implements OnClickListener {
    private final View.OnClickListener mListener;

    public Clickable(View.OnClickListener l) {
        mListener = l;
    }

    @Override
    public void onClick(View v) {
        mListener.onClick(v);
    }
}

class MyURLSpan extends ClickableSpan {

    private String mUrl;
    private Context ctx;

    MyURLSpan(String url, Context context) {
        mUrl = url;
        ctx = context;
    }

    @Override
    public void onClick(View widget) {
        if (mUrl.equals("www.baidu.com")) {
            Toast.makeText(ctx, mUrl, Toast.LENGTH_LONG).show();
            widget.setBackgroundColor(Color.parseColor("#00000000"));

        }
    }
}

你可能感兴趣的:(textview 单击事件)