private void initAgreement(TextView text) {
text.setClickable(true);
text.setText(getClickableSpan(text));
text.setMovementMethod(LinkMovementMethod.getInstance());
}
private SpannableString getClickableSpan(TextView textView) {
View.OnClickListener listener1 = new View.OnClickListener() {
// 如下定义自己的动作
public void onClick(View v) {
Intent intent = new Intent(AboutActivity.this, ProtocalActivity.class);
intent.putExtra(ProtocalActivity.TITLE_KEY, getString(R.string.user_agreement_content_href1));
intent.putExtra(ProtocalActivity.CONTENT_URI_KEY, ProtocalActivity.END_USER_LICENSE_AGREEMENT);
startActivity(intent);
}
};
String agreement = textView.getText().toString();
String h1 = getResources().getString(R.string.user_agreement_content_href1);
int h1Index = agreement.indexOf(h1);
SpannableString spanableInfo = new SpannableString(agreement);
int start1 = h1Index;
if (start1 != -1) {
int end1 = start1 + h1.length();
spanableInfo.setSpan(new Clickable(listener1), start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanableInfo.setSpan(new ForegroundColorSpan(Color.parseColor("#28c0c6")), start1, end1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spanableInfo;
}
class Clickable extends ClickableSpan implements OnClickListener {
private final View.OnClickListener mListener;
public Clickable(View.OnClickListener l) {
mListener = l;
}
@Override
public void onClick(View v) {
mListener.onClick(v);
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}