如何为单个textview设置多个单击事件?

 泥泥的春天_565_576 发布于 2023-01-21 23:30

我有一个textview,如下所示:

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

这只是一个大文本.所以,我用marquee水平滚动文本.工作正常.我的问题是,如何在单击选定的滚动文本时调用click事件.

说出来:

    当用户在上面的textview中单击"注册"一词时,我必须调用新的Intent.

    当用户单击该单词时"Terms",我必须调用另一个新的Intent(具有术语的webview活动URL Link).

由于"注册"和"术语"这个词是Web URL,我尝试过如下所示:

    String mRegDesc = "By registering you agree to the " + "Terms of Use " + "and " + "Privacy Policy ";

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

上面的代码工作正常,当我点击"条款"这个词时它会把我带到浏览器但是我希望转到新的活动.

1 个回答
  • 最后,

    我找到了解决方案,

    这是解决方案:

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

    感谢Shayan pourvatan.

    2023-01-26 09:17 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有