SpannableString中的LinkMovementMethod和ClickableSpan的实现安卓可点击有颜色文字使用

安卓SpannableString中的LinkMovementMethod和ClickableSpan的实现可点击有颜色文字

1. 使用自定义ClickableSpan生成可点击不带下滑线文字

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.text.SpannableString;
    import android.text.Spanned;
    import android.text.TextPaint;
    import android.text.method.LinkMovementMethod;
    import android.text.style.ClickableSpan;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            TextView mTextView = (TextView) findViewById(R.id.tv_show);

            String text = "There is no charm equal to tenderness of heart.";
            SpannableString sp = new SpannableString(text);

            MyClickableSpan clickableSpan = new MyClickableSpan() {

                @Override
                public void onClick(View widget) {
                    Toast.makeText(MainActivity.this, "charm", Toast.LENGTH_SHORT).show();
                }
            };


    ```
    // Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 为前后都不包含,表示如果
    // 后面在变色字段前后加入字符,加入的字符不会被包括,就不会跟着变色
    ```

            sp.setSpan(clickableSpan, text.indexOf("charm"), text.indexOf("charm") + "charm".length(),  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            mTextView.setMovementMethod(LinkMovementMethod.getInstance());
            mTextView.setText(sp);
        }

        class MyClickableSpan extends ClickableSpan {

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(false);
            }

            @Override
            public void onClick(View widget) {

            }
        }}

2. 使用自定义LinkMovementMethod生成不可滑动的SpannableString文字

// 有时候点击ClickableSpan会出现文本抖动滑动现象,这是系统LinkMovementMethod默认自带的效果,下面的MyLinkMovementMethod 类是重写LinkMovementMethod的ontouch方法,去掉了滑动抖动效果

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.text.SpannableString;
    import android.text.Spanned;
    import android.text.TextPaint;
    import android.text.method.LinkMovementMethod;
    import android.text.style.ClickableSpan;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView mTextView = (TextView) findViewById(R.id.tv_show);

    String text = "There is no charm equal to tenderness of heart.";
    SpannableString sp = new SpannableString(text);

    MyClickableSpan clickableSpan = new MyClickableSpan() {

        @Override
        public void onClick(View widget) {
            Toast.makeText(MainActivity.this, "charm", Toast.LENGTH_SHORT).show();
        }
    };

    // Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 为前后都不包含,表示如果后面在变色字段前后加入字符,加入的字符不会被包括,就不会跟着变色

    sp.setSpan(clickableSpan, text.indexOf("charm"), text.indexOf("charm") + "charm".length(),  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    mTextView.setMovementMethod(new MyLinkMovementMethod());
    mTextView.setText(sp);
    }

    class MyClickableSpan extends ClickableSpan {

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setColor(Color.BLUE);
        ds.setUnderlineText(false);
    }

    @Override
    public void onClick(View widget) {

    }
    }}


        class MyLinkMovementMethod extends LinkMovementMethod { 

        @Override
        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {

         int action = event.getAction();

            if (action == MotionEvent.ACTION_UP ||
                action == MotionEvent.ACTION_DOWN) {
                int x = (int) event.getX();
                int y = (int) event.getY();

                x -= widget.getTotalPaddingLeft();
                y -= widget.getTotalPaddingTop();

                x += widget.getScrollX();
                y += widget.getScrollY();

                Layout layout = widget.getLayout();

                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);

                Log.i("test", "line"+line);
                Log.i("test", "off"+off);
                ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

                if (link.length != 0&&action == MotionEvent.ACTION_UP) {

                        link[0].onClick(widget);

                    }

                    return true;
                } else {
                    Selection.removeSelection(buffer);
                }
            }

            return true;
    }

你可能感兴趣的:(SpannableString中的LinkMovementMethod和ClickableSpan的实现安卓可点击有颜色文字使用)