我正在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);
如您所见,我将 设置clickablespan
为TextView
从字符 12 到 17,并且我想在onClick
事件中获取这些字符。
反正我能做到吗?或者至少我可以将12, 17
参数传递给onClick
事件?
谢谢!