android 如何为单个文本视图设置多个单击事件?

zhte4eai  于 2023-01-19  发布在  Android
关注(0)|答案(5)|浏览(116)

我有一个文本视图,如下所示:

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

这只是一个很大的文本。所以,我用字幕水平滚动文本。这工作得很好。我的问题是,如何调用单击事件,同时单击所选的滚动文本。
例如:
1.当用户在上面的文本视图中点击单词***“Registering”时,我必须调用新的Intent。
1.当用户点击单词
"Terms"***时,我必须调用另一个新Intent(术语为webview的Activity具有URL Link)。
由于“Registering”和“Terms”都是Web URL,我尝试了如下操作:

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

上面的代码工作正常,它把我带到浏览器时,我点击“条款”一词,但我希望去新的活动。

6qftjkof

6qftjkof1#

最后,
我找到了解决办法,
解决方案如下:

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

多亏了沙扬·普尔瓦坦。

vmdwslir

vmdwslir2#

假设这是完整的字符串

注册即表示我同意条款和隐私政策

你想让它成为可点击的字符串是

条件条款隐私政策

所以,这是我的窍门。

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

    }
};

其主要功能

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

这里是函数调用

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

xzabzqsa3#

使用此选项,在单个TextView中单击两次即可使用此选项

步骤1-:文本将位于SpannableString中

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

步骤2:-在ClickableSpan中添加单击,如下所示

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

最后一步添加您对SpannableString的点击,字符开始和结束索引,如在第3个位置开始和在11个位置结束的注册单词,因此添加注册单词的点击

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

相同的术语在此之后添加您的SpannableString在您的TextView

textview.setMovementMethod(LinkMovementMethod.getInstance());
    textview.setText(ss, TextView.BufferType.SPANNABLE);
    textview.setSelected(true);
omvjsjqw

omvjsjqw4#

我建议在TextView中使用下面的代码来实现可点击的字符串,它是动态的。这个代码的优点是如果你多次点击相同的String,你可以点击两个Strings。例如,如果你想设置点击和字符串是**Boy正在玩板球。Boy正在玩足球**。**Boy**是两个单词都可以点击。

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

    }
j2datikz

j2datikz5#

此外,如果您想知道用户动态单击了哪个文本,请使用下面的

ClickableSpan listener = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                TextView tv = (TextView) textView;
                Spanned s = (Spanned) tv.getText();
                int start = s.getSpanStart(this);
                int end = s.getSpanEnd(this);
                String clickedText = s.toString().substring(start,end);
            }
        };

相关问题