【发布时间】:2026-01-17 23:40:01
【问题描述】:
我想将 onTouchListeners 分配给 TextView 中的每个单词。 (不链接到互联网上的东西,而只是为了继续应用程序内的游戏逻辑)。此时我的游戏的一般动作是查看一个 TextView,触摸一个单词,如果它是您赢得的目标单词,否则根据您触摸的单词加载另一个 TextView 并重复。我现在实现这一点的方法是为每个单词使用 ClickableSpans 和 onClicks。
但我更喜欢 onTouchListeners,这样我就可以在 touch_down 上更改单词背景的颜色,并在 touch_up 上执行游戏逻辑,使其看起来更具响应性。我怎样才能做到这一点?
final TextView defTV = (TextView) findViewById(R.id.defTV);
text = new SpannableString(rv); // rv is the future clickable TextView text
ClickableSpan clickableSpan = null;
String regex = "\\w+";
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(text);
while (matcher.find()) {
final int begin = matcher.start();
final int end = matcher.end();
clickableSpan = new ClickableSpan() {
public void onClick(View arg0) {
String lword = (String) text.subSequence(begin, end).toString();
if (lword.equalsIgnoreCase(targetword)) {
// WIN
} else {
// Build new TextView based on lword, start over
}
}
};
text.setSpan(clickableSpan, begin, end, 0);
}
【问题讨论】:
标签: android textview ontouchlistener spannablestring