SpannableStringBuilder,ClickSpan 不工作,clickspan 到多个字符串 android

SpannableStringBuilder , ClickSpan not working , clickspan to multiple strings android

我正在尝试使用 spannable 生成器实现对单个 TextView 的多次单击,我尝试了多种方法来实现对 spannable 字符串的单击但失败了,请指导我done/or 在这里做的事情。

            <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.setOnClickListener { // binding.cbFinancialLiability.toggle() (它作为 CheckedTextView).isChecked = !(它作为 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 内添加了点击,它开始正常工作,还添加了这一行 binding.termAndCondtionLabel.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 ")
        }
    }