Control onclicklistener in autolink enabled textview

前端 未结 9 2601
一向
一向 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

    one of the @CommonsWare post helps to intercept autolink OnClick event.

    private void fixTextView(TextView tv) {
        SpannableString current = (SpannableString) tv.getText();
        URLSpan[] spans =
                current.getSpans(0, current.length(), URLSpan.class);
    
        for (URLSpan span : spans) {
            int start = current.getSpanStart(span);
            int end = current.getSpanEnd(span);
    
            current.removeSpan(span);
            current.setSpan(new DefensiveURLSpan(span.getURL()), start, end,
                    0);
        }
    }
    
    public static class DefensiveURLSpan extends URLSpan {
        private String mUrl;
    
        public DefensiveURLSpan(String url) {
            super(url);
            mUrl = url;
        }
    
        @Override
        public void onClick(View widget) {
            // openInWebView(widget.getContext(), mUrl); // intercept click event and do something.
            // super.onClick(widget); // or it will do as it is.
        }
    }
    

    Apply above code simply as below. It will go through all linkable texts and replace click events to above event handler.

    fixTextView(textViewContent);
    
    0 讨论(0)
  • 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

    <TextView android:layout_width="wrap_content" android:linksClickable="true"
    android:layout_height="wrap_content" android:id="@+id/tvInfo2" android:text="@string/url_link" />
    
    0 讨论(0)
  • 2020-12-08 19:48

    Just adding

    textView.setMovementMethod(CustomLinkMovementMethod.getInstance());

    to @binary's answer for those whose the method did not work with them

    0 讨论(0)
提交回复
热议问题