How to set the part of the text view is clickable

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

    You can use sample code. You want to learn detail about ClickableSpan. Please check this documentaion

      SpannableString myString = new SpannableString("This is example");
    
                ClickableSpan clickableSpan = new ClickableSpan() {
                        @Override
                        public void onClick(View textView) {
                            ToastUtil.show(getContext(),"Clicked Smile ");
                        }
                    };
    
            //For Click
             myString.setSpan(clickableSpan,startIndex,lastIndex,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    
            //For UnderLine
             myString.setSpan(new UnderlineSpan(),startIndex,lastIndex,0);
    
            //For Bold
            myString.setSpan(new StyleSpan(Typeface.BOLD),startIndex,lastIndex,0);
    
            //Finally you can set to textView. 
    
            TextView textView = (TextView) findViewById(R.id.txtSpan);
            textView.setText(myString);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
    

提交回复
热议问题