簡體   English   中英

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

[英]SpannableStringBuilder , ClickSpan not working , clickspan to multiple strings android

我正在嘗試使用 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.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 ")
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM