简体   繁体   English

使用“是/否”对话框拦截链接LinkMovementMethod

[英]Intercept link LinkMovementMethod with a Yes/No dialog

I have a standard LinkMovementMethod established in my TextView to push a web Activity of some sort when the user touches a link. 我在TextView建立了一个标准LinkMovementMethod ,以在用户触摸链接时推送某种形式的Web Activity。 However, I want to establish a "do you want to see the link" dialog rather than taking the user straight to the webpage. 但是,我想建立一个“您是否想看到链接”对话框,而不是直接将用户带到网页。 I've tried overriding the touch methods but it all gets a little convoluted. 我尝试覆盖触摸方法,但是这一切都有些令人费解。 A little help? 一点帮助?

You can accomplish it in two ways: 您可以通过两种方式完成此任务:

  • Create custom Spans : more complicated, but you can accomplish more customised text consisting of clickable parts (or bold, differently coloured etc). 创建自定义跨度 :比较复杂,但是您可以完成更多由可单击部分(或粗体,颜色不同等)组成的自定义文本。 To know more, check out ClickableSpan and SpannableStringBuilder 要了解更多信息,请查看ClickableSpanSpannableStringBuilder
  • Extend LinkMovementMethod to accept custom click listener 扩展LinkMovementMethod以接受自定义点击侦听器

In my opinion second solution is better in basic cases like yours. 我认为,在像您这样的基本情况下,第二种解决方案更好。 Here is how you can do it: 这是您可以执行的操作:

  1. Copy this java class: InternalLinkMovementMethod to your project 将此Java类: InternalLinkMovementMethod复制到您的项目中
  2. Add set the link movement method of your TextView to this custom one, providing a click listener: 将TextView的链接移动方法添加到此自定义方法中,提供一个点击侦听器:
OnLinkClickedListener clickListener = new OnLinkClickedListener() {
    @Override
    public boolean onLinkClicked(String linkText) {
        // here you can handle your click, eg show the dialog
        // `linkText` is the text being clicked (the link)
        // return true if handled, false otherwise
    }
}

yourTextView.setMovementMethod(new InternalLinkMovementMethod(clickListener));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM