我正在创建一个ClickableSpan,它正在显示 正确的文字下划线。但是点击没有注册。 你知道我做错了吗??? 谢谢,维克多 以下是代码段:
view.setText("This is a test");
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(View widget) {
log("Clicked");
}
};
view.getText().setSpan(span, 0, view.getText().length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
答案 0 :(得分:353)
您是否尝试在包含范围的TextView上设置MovementMethod?你需要这样做才能点击工作......
tv.setMovementMethod(LinkMovementMethod.getInstance());
答案 1 :(得分:4)
经过一些试验和错误后,设置tv.setMovementMethod(LinkMovementMethod.getInstance());
的顺序很重要。
这是我的完整代码
String stringTerms = getString(R.string.sign_up_terms);
Spannable spannable = new SpannableString(stringTerms);
int indexTermsStart = stringTerms.indexOf("Terms");
int indexTermsEnd = indexTermsStart + 18;
spannable.setSpan(new UnderlineSpan(), indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(getColor(R.color.theme)), indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Log.d(TAG, "TODO onClick.. Terms and Condition");
}
}, indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
int indexPolicyStart = stringTerms.indexOf("Privacy");
int indexPolicyEnd = indexPolicyStart + 14;
spannable.setSpan(new UnderlineSpan(), indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(getColor(R.color.theme)), indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Log.d(TAG, "TODO onClick.. Privacy Policy");
}
}, indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textViewTerms = (TextView) findViewById(R.id.sign_up_terms_text);
textViewTerms.setText(spannable);
textViewTerms.setClickable(true);
textViewTerms.setMovementMethod(LinkMovementMethod.getInstance());
答案 2 :(得分:2)
Kotlin实用功能:
fun setClickable(textView: TextView, subString: String, handler: () -> Unit, drawUnderline: Boolean = false) {
val text = textView.text
val start = text.indexOf(subString)
val end = start + subString.length
val span = SpannableString(text)
span.setSpan(ClickHandler(handler, drawUnderline), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
textView.linksClickable = true
textView.isClickable = true
textView.movementMethod = LinkMovementMethod.getInstance()
textView.text = span
}
class ClickHandler(
private val handler: () -> Unit,
private val drawUnderline: Boolean
) : ClickableSpan() {
override fun onClick(widget: View?) {
handler()
}
override fun updateDrawState(ds: TextPaint?) {
if (drawUnderline) {
super.updateDrawState(ds)
} else {
ds?.isUnderlineText = false
}
}
}
用法:
Utils.setClickable(textView, subString, {handleClick()})
答案 3 :(得分:0)
科特林的直接进路
val textHeadingSpannable = SpannableString(resources.getString(R.string.travel_agent))
val clickSpan = object : ClickableSpan(){
override fun onClick(widget: View) {
// Handel your click
}
}
textHeadingSpannable.setSpan(clickSpan,104,136,Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
tv_contact_us_inquire_travel_agent.movementMethod = LinkMovementMethod.getInstance()
tv_contact_us_inquire_travel_agent.text = textHeadingSpannable