部分可点击的TextVie

使用Android原有的类ClickableSpan实现该效果:

src - xxx.java

	tx_tips = (TextView) findViewById(R.id.tx_tips);

        String clickableSpanText = getString(R.string.resend);
        String tip = getString(
                R.string.send_message_success_tip_text,
                "[email protected]",
                clickableSpanText
        );

        ClickableSpan clickableSpan = new ClickableSpan(){//直接重写原有的点击方法
            @Override
            public void onClick(View v) {
                Toast.makeText(ActChangeBingingPhoneSuccess.this, "aaa", Toast.LENGTH_SHORT).show();
            }
        };

        SpannableStringBuilder ssb = new SpannableStringBuilder(tip);
        ssb.setSpan(
                clickableSpan,
                tip.length() - (clickableSpanText + "。").length(),
                tip.length() - "。".length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        //tx_tips.setAutoLinkMask(Linkify.ALL);//加上这一句邮箱也会变链接
        tx_tips.setMovementMethod(LinkMovementMethod.getInstance());
        tx_tips.setHighlightColor(getResources().getColor(android.R.color.transparent));//去除背景
        tx_tips.setText(ssb);


layout - xxx.xml:

TextView就是一个普通的TextView就行了,我的是直接从项目中取出来的:



res - srting.xml:

重新发送
请前往邮箱 (%1$s) 查收邮件并回复申请资料,如未收到邮件,请点%2$s。

效果是中间红色圈部分文字,蓝色字体可点击:


部分可点击的TextVie_第1张图片


补充:

重写ClickableSpan的updataDrawState方法可以获取画笔,调整可点击字体的样式:

(Windows Android Studio Alt + Insert)

部分可点击的TextVie_第2张图片


更改字体颜色并取消下划线代码:

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(getResources().getColor(R.color.text_blue));
                //设置可点击文本的字体颜色
                ds.setUnderlineText(false);
            }


最后,这位帅哥似乎讲的比较详细:点击打开链接。还没仔细看,我去吃午饭了。


你可能感兴趣的:(android)