用Spannable实现checkBox部分文字样式和点击

https://stackoverflow.com/questions/8184597/how-do-i-make-a-portion-of-a-checkboxs-text-clickable
http://www.cnblogs.com/sxzheng/p/4245873.html

[img]http://dl2.iteye.com/upload/attachment/0129/9758/e3d99229-c896-323e-9b6d-535bfcd44343.png[/img]

点击右边部分跳转,不影响CheckBox的勾选。


//图标和文字的间隔
String paddingLeftBlank = " ";

chk_agree = inflate.findViewById(R.id.chk_agree);
SpannableString ss = new SpannableString(paddingLeftBlank + chk_agree.getText());
ss.setSpan(new UnderlineSpan(), paddingLeftBlank.length() + 2, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.ca_blue)), paddingLeftBlank.length() + 2, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
if (view instanceof TextView)
((TextView) view).setHighlightColor(getResources().getColor(android.R.color.transparent));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view.cancelPendingInputEvents();
}
ToastUtil.showToast("go to license");
}

@Override
public void updateDrawState(TextPaint ds) {
// super.updateDrawState(ds);
}
}, paddingLeftBlank.length() + 2, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
CharSequence cs = TextUtils.expandTemplate("^1", ss);
chk_agree.setText(cs);
chk_agree.setMovementMethod(LinkMovementMethod.getInstance());

你可能感兴趣的:(android)