How to set multiple click event for the single textview?

后端 未结 4 1168
悲哀的现实
悲哀的现实 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);
    

提交回复
热议问题