clickable word inside TextView in android

后端 未结 6 2072
谎友^
谎友^ 2020-12-04 19:36

I have TextView with text that changed dynamically. This text contain strings like myWord. I want that after click to this \"

6条回答
  •  悲哀的现实
    2020-12-04 20:13

    You can use below code;

     SpannableString myString = new SpannableString(Html.fromHtml("Please "+""+"login"+"" +" or "+ ""+"sign up"+ ""+" to begin your YupIT experience"));
    
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View textView) {
                    Toast.makeText(getContext(),"dfsgvdfs",Toast.LENGTH_SHORT).show();
                }
            };
    
            ClickableSpan clickableSpan1 = new ClickableSpan() {
                @Override
                public void onClick(View textView) {
                    Toast.makeText(getContext(),"dfsgvdfs",Toast.LENGTH_SHORT).show();
                }
            };
    
            myString.setSpan(clickableSpan,6,12,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            myString.setSpan(clickableSpan1,15,23,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    
            myString.setSpan(new ForegroundColorSpan(Color.parseColor("#F15d36")),6, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            myString.setSpan(new ForegroundColorSpan(Color.parseColor("#F15d36")),15,23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    
            tvFound.setMovementMethod(LinkMovementMethod.getInstance());
            tvFound.setText(myString);
    

提交回复
热议问题