Control onclicklistener in autolink enabled textview

前端 未结 9 2608
一向
一向 2020-12-08 19:13

I am using a TextView for which I have set autolink=\"web\" property in XML file. I have also implemented the onClickListener for this TextView. Th

9条回答
  •  無奈伤痛
    2020-12-08 19:47

    Instead of using a onClickListener, you can try this.

    private void addLink() {
            tvLink = (TextView) findViewById(R.id.tvInfo2);
    
            String strURL = UrlLoader.getCodeUrl();
    
            // Make the url string clicable and take action in its onclick
            SpannableString spanUrl = SpannableString.valueOf(strURL);
            spanUrl.setSpan(new InternalURLSpan(new OnClickListener() {
                public void onClick(View v) {
    
                    //Do Some action
                }
            }), 0, spanUrl.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvLink.setText(spanUrl);
    
            // We probably also want the user to jump to your link by moving the
            // focus (e.g. using the trackball), which we can do by setting the
            // proper movement method:
            MovementMethod m = tvLink.getMovementMethod();
            if ((m == null) || !(m instanceof LinkMovementMethod)) {
                if (tvLink.getLinksClickable()) {
                    tvLink.setMovementMethod(LinkMovementMethod.getInstance());
                }
            }
        }
    

    Also in the layout XML file , dont forget to add

    
    

提交回复
热议问题