How to set the part of the text view is clickable

后端 未结 20 1035
无人共我
无人共我 2020-11-22 01:29

I have the text \"Android is a Software stack\". In this text i want to set the \"stack\" text is clickable. in the sense if you click on t

20条回答
  •  别那么骄傲
    2020-11-22 02:29

    For those that are looking for a solution in Kotlin here is what worked for me:

    private fun setupTermsAndConditions() {
        val termsAndConditions = resources.getString(R.string.terms_and_conditions)
        val spannableString = SpannableString(termsAndConditions)
        val clickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                if (checkForWifiAndMobileInternet()) {
                    // binding.viewModel!!.openTermsAndConditions()
                    showToast("Good, open the link!!!")
    
                } else {
                    showToast("Cannot open this file because of internet connection!")
                }
    
            }
    
            override fun updateDrawState(textPaint : TextPaint) {
                super.updateDrawState(textPaint)
                textPaint.color = resources.getColor(R.color.colorGrey)
                textPaint.isFakeBoldText = true
            }
        }
    
        spannableString.setSpan(clickableSpan, 34, 86, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.tvTermsAndConditions.text = spannableString
        binding.tvTermsAndConditions.movementMethod = LinkMovementMethod.getInstance()
        binding.tvTermsAndConditions.setHighlightColor(Color.TRANSPARENT);
    
    }
    

提交回复
热议问题