Android ClickableSpan get text onClick()

后端 未结 4 1876
陌清茗
陌清茗 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: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) + "]");
        }
    }
    

提交回复
热议问题