How to set the part of the text view is clickable

后端 未结 20 1020
无人共我
无人共我 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:22

    i coded an example to solve your question in Kotlin.

    This is the Code:

        val completeText = getString(R.string.terms_description)
        val textToFind = getString(R.string.show_terms)
        val spannableString: Spannable = SpannableString(completeText)
        val startFocus = completeText.indexOf(textToFind)
        val endFocus = startFocus + textToFind.length
    
        spannableString.setSpan(object: ClickableSpan() {
            override fun onClick(p0: View) {
                showMessage()
            }
        }, startFocus, endFocus, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        show_terms.text = spannableString
        show_terms.movementMethod = LinkMovementMethod.getInstance();
        show_terms.highlightColor = Color.TRANSPARENT;
    

    This is the XML

        
    
        
    

    This is how it looks

    enter image description here

提交回复
热议问题