text_click = getString(R.string.choose_monitor_text_click);
text_phone_content = getString(R.string.choose_monitor_text_phone) + text_click;
text_headPad_content = getString(R.string.choose_monitor_text_headpad) + text_click;
int start_phoneContent = getString(R.string.choose_monitor_text_phone).length();
int end_phoneContent = text_phone_content.length();
int start_headPadContent = getString(R.string.choose_monitor_text_headpad).length();
int end_headPadContent = text_headPad_content.length();
SpannableStringBuilder builder_phone = new SpannableStringBuilder(text_phone_content);
SpannableStringBuilder builder_headPad = new SpannableStringBuilder(text_headPad_content);
builder_phone.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
// TODO Auto-generated method stub
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
text_phone.setHighlightColor(getResources().getColor(android.R.color.transparent));
Intent intent = new Intent(context, SleepexplainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_righttoleft, R.anim.out_righttoleft);
}
}, start_phoneContent, end_phoneContent, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder_headPad.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
// TODO Auto-generated method stub
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
text_headPad.setHighlightColor(getResources().getColor(android.R.color.transparent));
Intent intent = new Intent(context, SleepExplain4HeadPadActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_righttoleft, R.anim.out_righttoleft);
}
}, start_headPadContent, end_headPadContent, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text_phone.setText(builder_phone);
text_phone.setMovementMethod(LinkMovementMethod.getInstance());// 使用ClickableSpan的点击事件要设置这个才有效
text_headPad.setText(builder_headPad);
text_headPad.setMovementMethod(LinkMovementMethod.getInstance());
Android中为TextView中的部分文字设置颜色和点击事件
最新推荐文章于 2019-10-29 19:00:29 发布