我正在尝试使用spannable builder实现单个TextView上的多次点击,我尝试了多种方法来实现点击spannable字符串,但失败了,请指导我我做了什么/或在这里做错了。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/figma_10_dp"
android:layout_marginBottom="@dimen/figma_32_dp">
<androidx.appcompat.widget.AppCompatCheckedTextView
android:id="@+id/cb_terms_of_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="@dimen/sdp_16"
android:layout_marginTop="@dimen/sdp_12"
android:layout_marginBottom="@dimen/sdp_31"
android:drawableLeft="@drawable/tos_check_box_selector"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="@+id/tv_terms_of_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/sdp_14"
android:layout_marginTop="@dimen/sdp_12"
android:layout_marginEnd="@dimen/sdp_25"
android:layout_toEndOf="@+id/cb_terms_of_service"
android:layout_toRightOf="@+id/cb_terms_of_service"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/cb_terms_of_service"
app:layout_constraintTop_toTopOf="@id/cb_terms_of_service">
<androidx.appcompat.widget.AppCompatTextView
style="@style/w_reg_grey_black_12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:includeFontPadding="false"
android:clickable="true"
android:id="@+id/termAndCondtionLabel"
android:text="I hereby accept that I have read and agree to the Terms and Conditions and Key Fact Statement "
android:textColor="@color/standardBlack" />
<androidx.appcompat.widget.AppCompatTextView
style="@style/w_semi_bold_black2_32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_3sdp"
android:gravity="center_vertical"
android:includeFontPadding="false"
android:text="Terms and Conditions"
android:textColor="@color/red_daily_limit"
android:textSize="@dimen/_10ssp"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
binding.bottomButtonLayout.cbTermsOfService.movementMethod = LinkMovementMethod.getInstance()
binding.bottomButtonLayout.cbTermsOfService.setOnClickListener {
// binding.cbFinancialLiability.toggle()
(it as CheckedTextView).isChecked = !(it as CheckedTextView).isChecked
binding.bottomButtonLayout.btnGetstartedSmsm.isEnabled =
(it as CheckedTextView).isChecked
}
private fun updateTermsAndCondition(){
// val builder = SpannableStringBuilder()
// val unClickableSpan = SpannableString("")
// val span = SpannableString(" ")
// builder.append(span);
// builder.setSpan(clickableSpan, firstSpan.length, firstSpan.length+secondSpan.length+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
// view.setText(builder,TextView.BufferType.SPANNABLE)
val completeText = "I hereby accept that I have read and agree to the Terms and Conditions and Key Fact Statement"
val termsTextToFind = "Terms and Conditions"
val keyFactTextToFind = "Key Fact Statement"
val spannableString: Spannable = SpannableString(completeText)
val startFocusTerms = completeText.indexOf(termsTextToFind)
val endFocusTerms = startFocusTerms + termsTextToFind.length
val startFocusKey = completeText.indexOf(keyFactTextToFind)
val endFocusKey = startFocusKey + keyFactTextToFind.length
spannableString.setSpan(object: ClickableSpan() {
override fun onClick(view: View) {
showTermsAndConditions()
}
}, startFocusTerms, endFocusTerms, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableString.setSpan(
StyleSpan(Typeface.BOLD),
startFocusTerms, endFocusTerms,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(ForegroundColorSpan(ContextCompat.getColor(binding.termAndCondtionLabel.context,R.color.red_daily_limit)), startFocusTerms, endFocusTerms, 0)
spannableString.setSpan(object: ClickableSpan() {
override fun onClick(view: View) {
showKeys()
}
}, startFocusKey, endFocusKey, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
spannableString.setSpan(
StyleSpan(Typeface.BOLD),
startFocusKey, endFocusKey,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(ForegroundColorSpan(ContextCompat.getColor(binding.termAndCondtionLabel.context,R.color.red_daily_limit)),
startFocusKey, endFocusKey, 0)
// show_terms.text = spannableString
binding.termAndCondtionLabel.text = spannableString
binding.termAndCondtionLabel.text = buildSpannedString {
append("I hereby accept that I have read and agree to the ")
inSpans(
ForegroundColorSpan(ContextCompat.getColor(binding.termAndCondtionLabel.context,R.color.red_daily_limit))
){
bold {
append("Terms and Conditions ")
}
object :ClickableSpan(){
override fun onClick(view: View) {
showTermsAndConditions()
}
}
}
append(" and ")
inSpans(
object :ClickableSpan(){
override fun onClick(view: View) {
showKeys()
}
},
ForegroundColorSpan(ContextCompat.getColor(binding.termAndCondtionLabel.context,R.color.red_daily_limit))
){
bold {
append("Key Fact Statement ")
}
}
}
问题出在这一行
{
bold {
append("Terms and Conditions ")
}
object :ClickableSpan(){
override fun onClick(view: View) {
showTermsAndConditions()
}
}
}
在span内添加了click,它开始正常工作,并且还添加了这行binding. termandconditionlabel。movementMethod = LinkMovementMethod.getInstance()
inSpans(
object :ClickableSpan(){
override fun onClick(view: View) {
showKeys()
}
override fun updateDrawState(ds: TextPaint) { //to remove line //from spannable string
super.updateDrawState(ds)
ds.isUnderlineText = false
}
},
ForegroundColorSpan(ContextCompat.getColor(binding.termAndCondtionLabel.context,R.color.red_daily_limit))
){
bold {
append("Key Fact Statement ")
}
}