今天看啥  ›  专栏  ›  tran_sient

安卓设置部分文字的点击事件

tran_sient  · CSDN  ·  · 2020-03-11 12:18

引言:
能用一个TextView解决的事情为什么要用两个呢。
方法:
1.两个TextView,甚至多个,可以实现…
2.正解来了,使用SpannableString或SpannableStringBuilder,这两个类专门用于字体样式设置,可以更改颜色,设置点击事件等。
用法:

        SpannableString spannableString=new SpannableString("This is a partly click text");
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(Main2Activity.this, "Yes!!!", Toast.LENGTH_SHORT).show();
            }

            /**
             * 重写此函数是为了去除默认的下划线,以及设置我们自己的颜色
             * @param ds 用于自定义样式的画笔对象
             */
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.YELLOW);
                ds.setUnderlineText(false);
            }
        },17,spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        mPartClick.setText(spannableString);
        mPartClick.setMovementMethod(LinkMovementMethod.getInstance());
        //注意,设置了ClickableSpan后,点击将会触发TextView在该区域的高亮效果,即一个背景变化,我们手动去除
        mPartClick.setHighlightColor(getResources().getColor(android.R.color.transparent));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

解释:
1.核心setSpan函数接收四个参数,一个Object(这个参数很厉害,可以传入各种像 xxxSpan名称类的对象,用于设置点击事件或字体颜色等),一个start参数(从哪个文字开始),一个end参数(从哪个文字结束),一个flag参数。
这里说一下flag参数,一般设置为:

/**
     * Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand
     * to include text inserted at their starting point but not at their
     * ending point.  When 0-length, they behave like marks.
     */
    public static final int SPAN_INCLUSIVE_EXCLUSIVE = SPAN_MARK_MARK;

    /**
     * Spans of type SPAN_INCLUSIVE_INCLUSIVE expand
     * to include text inserted at either their starting or ending point.
     */
    public static final int SPAN_INCLUSIVE_INCLUSIVE = SPAN_MARK_POINT;

    /**
     * Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand
     * to include text inserted at either their starting or ending point.
     * They can never have a length of 0 and are automatically removed
     * from the buffer if all the text they cover is removed.
     */
    public static final int SPAN_EXCLUSIVE_EXCLUSIVE = SPAN_POINT_MARK;

    /**
     * Non-0-length spans of type SPAN_EXCLUSIVE_INCLUSIVE expand
     * to include text inserted at their ending point but not at their
     * starting point.  When 0-length, they behave like points.
     */
    public static final int SPAN_EXCLUSIVE_INCLUSIVE = SPAN_POINT_POINT;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

这四个常量,分别代表了
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE - 不包含两端start和end所在的端点
Spanned.SPAN_EXCLUSIVE_INCLUSIVE - 不包含端start,但包含end所在的端点
Spanned.SPAN_INCLUSIVE_EXCLUSIVE - 包含两端start,但不包含end所在的端点
Spanned.SPAN_INCLUSIVE_INCLUSIVE - 包含两端start和end所在的端点
实际上我操作起来,发现并没有什么区别好像。。。
2.若为TextView设置了点击事件,我们部分点击将会失效。
3.设置了部分点击,还需setMovementMethod之后,才能生效,即响应点击事件,原因暂不明确。




原文地址:访问原文地址
快照地址: 访问文章快照