Android同一个TevtView显示不同颜色的文字,添加不同的点击事件。

来源:互联网 发布:河池市网络问政平台 编辑:程序博客网 时间:2024/04/28 16:59

一下代码在同一个Textview里面设置了2段不同的文字和点击事件。

TextView protocalTv=(TextView)findViewById(R.id.protocal);String str=protocalTv.getText().toString();SpannableString spannableString1 = new SpannableString(str);spannableString1.setSpan(new ClickableSpan(){    public void onClick(View widget) {        com.caimao.gjs.utils.WebviewUtils.showRiskBook(OpenAccountThird.this) ;//风险揭示书    }}, str.length()-5, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);spannableString1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_blue)), str.length() - 5, str.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);spannableString1.setSpan(new ClickableSpan(){    public void onClick(View widget) {        com.caimao.gjs.utils.WebviewUtils.showRelevantAgreement(OpenAccountThird.this);  //电子化相关协议    }}, 7,14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);spannableString1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_blue)), 7, 14, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);protocalTv.setText(spannableString1);protocalTv.setMovementMethod(LinkMovementMethod.getInstance());

0 0
原创粉丝点击