20

我正在ClickableSpan使用 TextView,并且正在尝试获取单击的跨度的文本。这是我的代码。

// this is the text we'll be operating on
SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");

// make "dolor" (characters 12 to 17) display a toast message when touched
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View view) {
        // This will get "Lorem ipsum dolor sit amet", but I just want "dolor"
        String text = ((TextView) view).getText().toString(); 
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }
};

text.setSpan(clickableSpan, 12, 17, 0);

如您所见,我将 设置clickablespanTextView从字符 12 到 17,并且我想在onClick事件中获取这些字符。

反正我能做到吗?或者至少我可以将12, 17参数传递给onClick事件?

谢谢!

4

4 回答 4

40

尝试这个:

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) + "]");
    }
}
于 2013-11-03T07:18:43.730 回答
5

稍微简单一点,必要时也可以传递模型引用。

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 + "]");
    }
}

然后调用 new SpecialClickableSpan("My Text")

于 2014-01-30T22:34:32.680 回答
3

编辑:以前的代码是错误的,这有效

    // 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();
                }
            }
        }
    };
于 2013-11-03T07:07:12.720 回答
0

您也可以像这样使用使字符串可跨越

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);
    }

CustomerTextClick将是

私有静态类 CustomerTextClick 扩展 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
    }
}

经过测试和工作的代码。

于 2016-04-06T08:19:22.097 回答