Android中为TextView中的部分文字设置颜色和点击事件

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());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值