Android ClickableSpan get text onClick()

后端 未结 4 1860
陌清茗
陌清茗 2020-12-05 14:46

I\'m working on ClickableSpan in a TextView, and I\'m trying to get the clicked span\'s text. This is my code.

// this is the text we\'ll be ope         


        
相关标签:
4条回答
  • 2020-12-05 15:11

    Edited: previous code was wrong, this works

        // make "dolor" (characters 12 to 17) display a toast message when touched
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                TextView textView = (TextView) view;
                CharSequence charSequence = textView.getText();
                if (charSequence instanceof Spannable) {
                    Spannable spannableText = (Spannable)charSequence;
                    ClickableSpan[] spans = spannableText.getSpans(0, textView.length(), ClickableSpan.class);
                    for (ClickableSpan span : spans) {
                        int start = spannableText.getSpanStart(span);
                        int end = spannableText.getSpanEnd(span);
                        Toast.makeText(MainActivity.this, charSequence.subSequence(start, end), Toast.LENGTH_LONG).show();
                    }
                }
            }
        };
    
    0 讨论(0)
  • 2020-12-05 15:12

    A little simpler, could also pass a model reference if necessary.

    public class SpecialClickableSpan extends ClickableSpan {
    
        String text;
    
        public SpecialClickableSpan(String text){
             super();
             this.text = text;
        }
    
        @Override
        public void onClick(View widget) {
             Log.d(TAG, "onClick [" + text + "]");
        }
    }
    

    Then call new SpecialClickableSpan("My Text")

    0 讨论(0)
  • 2020-12-05 15:14

    You can also use to make string spannable like this

    String htmlLinkText = "Lorem ipsum <a href='http://www.google.com'>dolor</a> sit amet";
        testView.setText(Html.fromHtml(htmlLinkText));
        testView.setMovementMethod(LinkMovementMethod.getInstance());
    
        CharSequence text = testView.getText();
        if (text instanceof Spannable) {
            int end = text.length();
            Spannable sp = (Spannable) testView.getText();
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.clearSpans();//should clear old spans
            for (URLSpan url : urls) {
                CustomerTextClick click = new CustomerTextClick(url.getURL());
                style.setSpan(click, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            testView.setText(style);
        }
    

    and CustomerTextClick will be

    private static class CustomerTextClick extends ClickableSpan {

        private String mUrl;
    
        CustomerTextClick(String url) {
            mUrl = url;
        }
    
        @Override
        public void onClick(View widget) {
            // TODO Auto-generated method stub
            //Toast.makeText(ctx, "hello google!",Toast.LENGTH_LONG).show();
            // Do your action here
        }
    }
    

    Tested and working code.

    0 讨论(0)
  • 2020-12-05 15:17

    try this:

    public class LoremIpsumSpan extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            // TODO add check if widget instanceof TextView
            TextView tv = (TextView) widget;
            // TODO add check if tv.getText() instanceof Spanned
            Spanned s = (Spanned) tv.getText();
            int start = s.getSpanStart(this);
            int end = s.getSpanEnd(this);
            Log.d(TAG, "onClick [" + s.subSequence(start, end) + "]");
        }
    }
    
    0 讨论(0)
提交回复
热议问题