2017-08-26 2 views
3

Je veux savoir comment puis-je détecter certains mots spécifiés dans SpannableStringBuilder. Mon but est de changer la couleur de ces mots.Comment détecter des mots spécifiques dans SpannableStringBuilder android?

Dire que j'ai SpannableStringBuilder span et la liste inclus mes mots comme « Nom », « famille », « Location » etc.

Dans ce stade, je veux vérifier si mon span ces mots est, y compris changent alors de couleur .

Par exemple, je veux quelque chose comme:

if(span.contain("Name")) // Change Color of the word "Name" every where in this span 
if(span.contain("Family")) // Change Color of the word "Family" every where in this span 

et ainsi ...

est-il une méthode pour vérifier? tout exemple de code sera apprécié.

+0

span.toString() indexOf ("Nom"); span.setSpan (...) –

+0

J'ai posté une réponse complète pour le rendre plus clair. –

Répondre

2

Il n'y a pas de méthode de recherche dans SpannableStringBuilder mais vous pouvez utiliser indexOf() après avoir converti à un String.

Set<String> words = new HashSet<String>() {{ 
    add("Name"); add("Family"); add("Location"); 
}}; 
String s = span.toString(); 
for (String word : words) { 
    int len = word.length(); 
    // Here you might want to add a check for an empty word 
    for (int i = 0; (i = s.indexOf(word, i)) >= 0; i += len) { 
    span.setSpan(new ForegroundColorSpan(Color.BLUE), i, i + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    } 
} 
+0

Merci cela fonctionne mais pas parfaitement, par exemple lorsque mon Span est "Nom, Nommez-le "Nomme le dernier nom pas tous les mots spécifiés (dans ce cas Nom) –

+1

hey j'ai eu le problème, s'il vous plaît changer' span.setSpan (bleu, i, i + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 'à' span .setSpan (nouveau ForegroundColorSpan (Color.BLUE), je, je + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 'alors je vais accepter la réponse;) –

+0

@shahingh: Cool, merci! Modifié. Une version précédente était déjà comme ça mais je pensais que c'était plus efficace d'extraire la variable. Bon à savoir ça ne marche pas :) –

1

Voici comment faire plusieurs couleurs et le mot:

SpannableStringBuilder builder = new SpannableStringBuilder(); 
String red = "this is red"; 
SpannableString redSpannable= new SpannableString(red); 
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0); 
builder.append(redSpannable); 
String white = "this is white"; 
SpannableString whiteSpannable= new SpannableString(white); 
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0); 
builder.append(whiteSpannable); 
String blue = "this is blue"; 
SpannableString blueSpannable = new SpannableString(blue); 
blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0); builder.append(blueSpannable); mTextView.setText(builder, BufferType.SPANNABLE); 

Pour trouver des index de texte ne comme suit:

int first = str.indexOf("hi"); 
int next = str.indexOf("hi", first+1); 

read api

Faites ceci pour obtenir tous les index et les colorer

+0

je vous remercie, je fixe ma durée une fois, et je veux un moyen de vérifier les mots spécifiés après le réglage, tout comme le ** Gil vegliach ** –