अद्यतन के लिए यहाँ देखें: एक और बेहतर दृष्टिकोण 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);
}
};
}
पुराने उत्तर
मैं में क्लिक संभाल करना चाहता था मेरी अपनी गतिविधि मैंने कोड का पालन करके इसे हल किया:
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]);
}
स्रोत
2011-12-23 15:23:27
यदि आप श्रोताओं के समाधान को देख रहे हैं और इरादे नहीं हैं तो यह सबसे अच्छा जवाब है। यूआरएल या स्कीमा से जुड़ी सभी समस्याएं नहीं हैं। मेरे मामले में, मैं यह जानने के लिए एक सरल समाधान देख रहा था कि टेक्स्टव्यू में कौन सा शब्द टैप किया गया था। जब तक मैंने यह देखा तब तक कोई अच्छा समाधान नहीं था। यह समाधान बस पूरी तरह से काम करता है। यह वही बात करता है जो टेक्स्टव्यू क्लिक करने योग्य है लेकिन सबसे अच्छा हिस्सा यह है कि हम प्रासंगिक कार्रवाई करने के लिए नियंत्रण में हैं। मैंने उपरोक्त कोड के बावजूद एक बदलाव किया था। उपर्युक्त कोड के साथ, सभी शब्द लिंक के रूप में दिखाई देते हैं लेकिन मुझे वह नहीं चाहिए था। तो बस super.updateDrawState टिप्पणी की। –
धन्यवाद, मैं पिछले 2 दिनों से इस पर काम कर रहा हूं और अंततः मैं इसे आपके कोड का उपयोग करके हल करता हूं। फिर से धन्यवाद। –
धन्यवाद एम-वाजेएह। एक जादू की तरह काम किया। और उस अतिरिक्त बिट के लिए दीपक भी धन्यवाद जो मैं चाहता था। – Manu