Android: Setting onClickListener to a Part of text in a TextView - Issue

前端 未结 7 2253
忘掉有多难
忘掉有多难 2021-01-03 23:57

I am trying to recognise hashtags in my TextView and make them clickable such that I can take the user to another View when they click on the Hashtag.

I managed to i

7条回答
  •  渐次进展
    2021-01-04 00:43

    Yes you can do it, you need to use ClickableSpan with SpannableString

    paste this code inside your while loop

    final String tag = matcher.group(0);
    ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View textView) {
                        Log.e("click","click " + tag);
                    }
                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
    
                    }
                };
                hashText.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    

    Dont forget to set setMovementMethod() on your TextView

    holder.caption.setMovementMethod(LinkMovementMethod.getInstance());
    

提交回复
热议问题