How to set multiple click event for the single textview?

后端 未结 4 1172
悲哀的现实
悲哀的现实 2020-12-03 18:12

I have a textview as like the following:

txtByRegistering.setText(\"By Registering you agree to terms and condition and privacy policy\");

4条回答
  •  不知归路
    2020-12-03 18:47

    Let assume here is your complete string

    By Signing up, I agree to Terms of Conditions & Privacy Policy

    and string you want to make clickable is

    Terms of Conditions and Privacy Policy

    so, here is my trick.....

    ClickableSpan terms = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            new Utils(getActivity()).shortToast("Terms");
    
        }
    };
    
    ClickableSpan privacy = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            new Utils(getActivity()).shortToast("Privacy");
    
        }
    };
    

    the main function for this

    public void setClickableString(String wholeValue, TextView textView, final String[] clickableValue, ClickableSpan[] clickableSpans) {
        SpannableString spannableString = new SpannableString(wholeValue);
    
        for (int i = 0; i < clickableValue.length; i++) {
            ClickableSpan clickableSpan = clickableSpans[i];
            String link = clickableValue[i];
    
            int startIndexOfLink = wholeValue.indexOf(link);
            spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        textView.setHighlightColor(
                Color.TRANSPARENT); // prevent TextView change background when highlight
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString, TextView.BufferType.SPANNABLE);
    }
    

    and here is the function calling

    setClickableString(getString(R.string.terms_and_policy), tv_terms, new String[]{"Terms of Conditions", "Privacy Policy"}, new ClickableSpan[]{terms, privacy});
    

提交回复
热议问题