How to set multiple click event for the single textview?

后端 未结 4 1165
悲哀的现实
悲哀的现实 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:43

    Use this one it works for me two click in single TextView

    Step1-: Your text will be in SpannableString

    SpannableString ss = new SpannableString("By Registering you agree to terms and condition and privacy policy");
    

    Step2:-add click in ClickableSpan like this

     ClickableSpan Registering = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Intent intent=new Intent(this,WebView_Activity.class);
                                startActivity(intent);
            }
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(true);
            }
        };
        ClickableSpan terms = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
    
                Intent intent=new Intent(this,WebView_Activity.class);
                                startActivity(intent);
            }
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(true);
            }
        };
    

    Final step add your click on SpannableString with character starting and ending index like Registering word in start at 3rd position and end at 11 so add click for Registering word

     ss.setSpan(Registering , 3, 11, 0);
    

    same for term after this add your SpannableString on your TextView

      textview.setMovementMethod(LinkMovementMethod.getInstance());
        textview.setText(ss, TextView.BufferType.SPANNABLE);
        textview.setSelected(true);
    
    0 讨论(0)
  • 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});
    
    0 讨论(0)
  • 2020-12-03 18:56

    I would suggest below code for clickable string in TextView it is dynamic. Advantage of this code is if you have same String multiple times you can have click on both Strings. For example if you want to set click and String is Boy is playing cricket. Boy is playing football. Boy is two times both word will be clickable.

        public void setClicksOnString(String completeString, List<String> stringsToClick, TextView textView) {
            SpannableString spannableString = new SpannableString(completeString);
            for (int m = 0; m < stringsToClick.size(); m++) {
                Matcher matcher = Pattern.compile(stringsToClick.get(m)).matcher(spannableString);
                while (matcher.find()) {
                    ClickableSpan stringClick = new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                            //you compare the string and your click logics
    
                        }
                    };
                    spannableString.setSpan(stringClick, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
                }
            }
            textView.setHighlightColor(
                    Color.TRANSPARENT); // prevent TextView change background when highlight
            textView.setMovementMethod(LinkMovementMethod.getInstance());
            textView.setText(spannableString, TextView.BufferType.SPANNABLE);
    
        }
    
    0 讨论(0)
  • 2020-12-03 18:59

    Finally,

    I found the solution for that,

    Here is the solution :

        SpannableString SpanString = new SpannableString(
                "By Registering you agree to the Terms of Use and Privacy Policy");
    
        ClickableSpan teremsAndCondition = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
    
    
                Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
                mIntent.putExtra("isTermsAndCondition", true);
                startActivity(mIntent);
    
            }
        };
    
       // Character starting from 32 - 45 is Terms and condition. 
       // Character starting from 49 - 63 is privacy policy. 
    
        ClickableSpan privacy = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
    
                Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
                mIntent.putExtra("isPrivacyPolicy", true);
                startActivity(mIntent);
    
            }
        };
    
        SpanString.setSpan(teremsAndCondition, 32, 45, 0);
        SpanString.setSpan(privacy, 49, 63, 0);
        SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 32, 45, 0);
        SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 49, 63, 0);
        SpanString.setSpan(new UnderlineSpan(), 32, 45, 0);
        SpanString.setSpan(new UnderlineSpan(), 49, 63, 0);
    
        txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
        txtByRegistering.setText(SpanString, BufferType.SPANNABLE);
        txtByRegistering.setSelected(true);
    

    thanks to Shayan pourvatan.

    0 讨论(0)
提交回复
热议问题