【问题标题】:Android ClickableSpan not calling onClickAndroid ClickableSpan 没有调用 onClick
【发布时间】:2012-01-28 07:20:57
【问题描述】:

我正在创建一个 ClickableSpan,它可以正常显示 适当的文字加下划线。但是,点击未注册。 你知道我做错了吗???

谢谢,维克多

这里是sn-p的代码:

view.setText("This is a test");
ClickableSpan span = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        log("Clicked");
    }
};
view.getText().setSpan(span, 0, view.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

【问题讨论】:

    标签: android textview


    【解决方案1】:

    您是否尝试在包含跨度的 TextView 上设置 MovementMethod?您需要这样做才能使点击工作...

    tv.setMovementMethod(LinkMovementMethod.getInstance());
    

    【讨论】:

    • 如果tv 的类型为EditText,则无法正常工作,您可以单击span 但不能正常编辑。
    • 非常感谢!这对我也有用!你能解释一下这个设置的原因吗?
    • 当然,我需要设置文档称为“箭头键处理程序”的内容,以使点击处理程序工作。太明显了! (╯°□°)╯︵ ┻━┻
    • 它有效,但我永远不会真正知道为什么这不是默认行为。
    • 而谷歌忘了提到调用 setMovementMethod 会使“ellipsize”不起作用......所以似乎正确的方法是手动实现一个 TouchListener 并从那里获取它......
    【解决方案2】:

    Kotlin 实用函数:

    fun setClickable(textView: TextView, subString: String, handler: () -> Unit, drawUnderline: Boolean = false) {
        val text = textView.text
        val start = text.indexOf(subString)
        val end = start + subString.length
    
        val span = SpannableString(text)
        span.setSpan(ClickHandler(handler, drawUnderline), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    
        textView.linksClickable = true
        textView.isClickable = true
        textView.movementMethod = LinkMovementMethod.getInstance()
    
        textView.text = span
    }
    
    class ClickHandler(
            private val handler: () -> Unit,
            private val drawUnderline: Boolean
    ) : ClickableSpan() {
        override fun onClick(widget: View?) {
            handler()
        }
    
        override fun updateDrawState(ds: TextPaint?) {
            if (drawUnderline) {
                super.updateDrawState(ds)
            } else {
                ds?.isUnderlineText = false
            }
        }
    }
    

    用法:

    Utils.setClickable(textView, subString, {handleClick()})
    

    【讨论】:

      【解决方案3】:

      经过反复试验,设置tv.setMovementMethod(LinkMovementMethod.getInstance()); 的顺序确实很重要。

      这是我的完整代码

      String stringTerms = getString(R.string.sign_up_terms);
      Spannable spannable = new SpannableString(stringTerms);
      int indexTermsStart = stringTerms.indexOf("Terms");
      int indexTermsEnd = indexTermsStart + 18;
      spannable.setSpan(new UnderlineSpan(), indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      spannable.setSpan(new ForegroundColorSpan(getColor(R.color.theme)), indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      spannable.setSpan(new ClickableSpan() {
          @Override
          public void onClick(View widget) {
              Log.d(TAG, "TODO onClick.. Terms and Condition");
          }
      }, indexTermsStart, indexTermsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      
      int indexPolicyStart = stringTerms.indexOf("Privacy");
      int indexPolicyEnd = indexPolicyStart + 14;
      spannable.setSpan(new UnderlineSpan(), indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      spannable.setSpan(new ForegroundColorSpan(getColor(R.color.theme)), indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      spannable.setSpan(new ClickableSpan() {
          @Override
          public void onClick(View widget) {
              Log.d(TAG, "TODO onClick.. Privacy Policy");
          }
      }, indexPolicyStart, indexPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      
      TextView textViewTerms = (TextView) findViewById(R.id.sign_up_terms_text);
      textViewTerms.setText(spannable);
      textViewTerms.setClickable(true);
      textViewTerms.setMovementMethod(LinkMovementMethod.getInstance());
      

      【讨论】:

        【解决方案4】:

        Kotlin 中的直接方法

          val  textHeadingSpannable = SpannableString(resources.getString(R.string.travel_agent))
        
        
                   val clickSpan = object : ClickableSpan(){
                       override fun onClick(widget: View) {
        
                        // Handel your click
                       }
                   }
                    textHeadingSpannable.setSpan(clickSpan,104,136,Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        
                    tv_contact_us_inquire_travel_agent.movementMethod = LinkMovementMethod.getInstance()
                    tv_contact_us_inquire_travel_agent.text = textHeadingSpannable
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-14
          • 2016-10-24
          • 1970-01-01
          • 2017-10-28
          • 1970-01-01
          • 1970-01-01
          • 2020-04-15
          • 1970-01-01
          相关资源
          最近更新 更多