How to set multiple click event for the single textview?

做~自己de王妃 提交于 2019-11-26 09:57:24

问题


I have a textview as like the following:

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

It is just a big text. So, I used marquee to scroll the text horizontally. that works fine. My Question is, How to invoke the click event while clicking the selected scrolling text .

Say for ex :

  1. when user click the word \"Registering\" in the above textview, I have to invoke the new Intent.
  2. When the user click on the word \"Terms\" , I have to invoke another new Intent (An Activity with webview as Terms has URL Link).

As the word \"Registering\" and \"Terms\" are Web URLs, I tried something like below :

    String mRegDesc = \"By registering you agree to the \" + \"<a href=\\\"\"
            + Constant.URL + \"/terms_and_conditions\"
            + \"\\\">Terms of Use</a> \" + \"and \" + \"<a href=\\\"\" + Constant.URL
            + \"/privacy\" + \"\\\">Privacy Policy</a> \";

    txtByRegistering.setText(Html.fromHtml(mRegDesc));
    txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
    txtByRegistering.setSelected(true);
    txtByRegistering.setTypeface(mTyFaceOverLockReg, Typeface.BOLD);

The above code works fine and it brings me to the browser when i click the word \"Terms\" But i wish to go to new Activity.


回答1:


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.




回答2:


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});



回答3:


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);



回答4:


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);

    }


来源:https://stackoverflow.com/questions/21992735/how-to-set-multiple-click-event-for-the-single-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!