I would like to assign onTouchListeners to each word in a TextView. (Not to link to something on the internet, but just to continue the game logic inside the app). The general action of my game at this point is to see a TextView, touch a word, if it's the target word you win, else load another TextView based on the word you touch and repeat. The way I accomplish this now is with ClickableSpans and onClicks for each word.
But I would rather have onTouchListeners so I can change the color of the background of the word on touch_down and do the game logic on touch_up, to make it look more responsive. How can I accomplish this?
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);
}