wybierz słowo na dotknięcie w TextView / EditText
Jak wybrać słowo na kranu w TextView
/EditText
w android. Mam smsa w TextView
/EditText
a kiedy użytkownik dotknie słowa, Chcę, aby to słowo zostało wybrane, a następnie, gdy wywołuję metodę getSelectedText (), powinna zwrócić mi wybrane słowo.
Każda pomoc będzie mile widziana.
Moim celem jest wykonanie jakiejś akcji, gdy użytkownik dotknie danego słowa w TextView
/EditText
.
16
2 answers
UPDATE: innym lepszym podejściem jest użycie BreakIterator
:
private void init() {
String definition = "Clickable words in text view ".trim();
TextView definitionView = (TextView) findViewById(R.id.text);
definitionView.setMovementMethod(LinkMovementMethod.getInstance());
definitionView.setText(definition, BufferType.SPANNABLE);
Spannable spans = (Spannable) definitionView.getText();
BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
iterator.setText(definition);
int start = iterator.first();
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
.next()) {
String possibleWord = definition.substring(start, end);
if (Character.isLetterOrDigit(possibleWord.charAt(0))) {
ClickableSpan clickSpan = getClickableSpan(possibleWord);
spans.setSpan(clickSpan, start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
private ClickableSpan getClickableSpan(final String word) {
return new ClickableSpan() {
final String mWord;
{
mWord = word;
}
@Override
public void onClick(View widget) {
Log.d("tapped on:", mWord);
Toast.makeText(widget.getContext(), mWord, Toast.LENGTH_SHORT)
.show();
}
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
};
}
STARA ODPOWIEDŹ
Chciałem zająć się klikaniem we własnej aktywności. Rozwiązałem go za pomocą następującego kodu:
private void init(){
String definition = "Clickable words in text view ".trim();
TextView definitionView = (TextView) findViewById(R.id.definition);
definitionView.setMovementMethod(LinkMovementMethod.getInstance());
definitionView.setText(definition, BufferType.SPANNABLE);
Spannable spans = (Spannable) definitionView.getText();
Integer[] indices = getIndices(
definitionView.getText().toString(), ' ');
int start = 0;
int end = 0;
// to cater last/only word loop will run equal to the length of indices.length
for (int i = 0; i <= indices.length; i++) {
ClickableSpan clickSpan = getClickableSpan();
// to cater last/only word
end = (i < indices.length ? indices[i] : spans.length());
spans.setSpan(clickSpan, start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = end + 1;
}
}
private ClickableSpan getClickableSpan(){
return new ClickableSpan() {
@Override
public void onClick(View widget) {
TextView tv = (TextView) widget;
String s = tv
.getText()
.subSequence(tv.getSelectionStart(),
tv.getSelectionEnd()).toString();
Log.d("tapped on:", s);
}
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
};
}
public static Integer[] getIndices(String s, char c) {
int pos = s.indexOf(c, 0);
List<Integer> indices = new ArrayList<Integer>();
while (pos != -1) {
indices.add(pos);
pos = s.indexOf(c, pos + 1);
}
return (Integer[]) indices.toArray(new Integer[0]);
}
31
Author: M-WaJeEh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-12 06:52:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-12 06:52:15
Użyj Linkify , Użyj własnego wyrażenia regularnego, aby wywołać rutynę po kliknięciu słowa.
Zobacz tutaj przykład Wikiwordu i odpowiedni Blogpost
0
Author: st0le,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-23 05:52:00
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-23 05:52:00