Voglio cambiare il colore della parola specifica esperienza in TextView
: ImmagineCambiare il colore del testo Sfondo di parola specifica in un TextView
come di seguito
Voglio cambiare il colore della parola specifica esperienza in TextView
: ImmagineCambiare il colore del testo Sfondo di parola specifica in un TextView
come di seguito
Ecco il mio codice di lavoro che è possibile utilizzare per evidenziare alcune parti della stringa:
private void highlightTextPart(TextView textView, int index, String regularExpression) {
String fullText = textView.getText().toString();
int startPos = 0;
int endPos = fullText.length();
String[] textParts = fullText.split(regularExpression);
if (index < 0 || index > textParts.length - 1) {
return;
}
if (textParts.length > 1) {
startPos = fullText.indexOf(textParts[index]);
endPos = fullText.indexOf(regularExpression, startPos);
if (endPos == -1) {
endPos = fullText.length();
}
}
Spannable spannable = new SpannableString(fullText);
ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE });
TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(null, Typeface.BOLD_ITALIC, -1, blueColor, null);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN);
spannable.setSpan(textAppearanceSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(backgroundColorSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannable);
}
Poi nella vostra attività, chiamata come la seguente:
int index = 3;
String regularExpression = " ";
String text = "Hello StackOverflow From BNK!";
TextView textView = (TextView) findViewById(R.id.textView);
if (textView != null) {
textView.setText(text);
highlightTextPart(textView, index, regularExpression);
}
Prova questa
String str = "Some really long string goes in here";
Spannable spannable = new SpannableString(str);
spannable.setSpan(new BackgroundColorSpan(
getResources().getColor(android.R.color.black)
), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(spannable);
vedi questo link
voglio cambiare con la parola specifico come String Def = rush_searh.getDefinition(), sostituire ("Presto". , "setSpan di Spannable & anche sapere come è necessario che il numero di inizio e fine (come in anser 3 & 7 in BackgroundColorSpan
UPDATE
Utilizza questo codice di lavoro
String mainString = "As Soon As";
String subString = "Soon";
if(mainString.contains(subString)) {
int startIndex = mainString.indexOf(subString);
int endIndex = startIndex + subString.length();
SpannableString spannableString = new SpannableString(mainString);
spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#ff0000")), startIndex, endIndex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
Voglio cambiare con una parola specifica come String Def = rush_searh.getDefinition(). Replace ("Soon", " So"); tv_term.setText (Html.fromHtml (Def)); –
Che cos'è ToolBarSpan? –
@Chirag si prega di consultare l'aggiornamento. –
Questa risposta deve essere contrassegnata come accettata. –
Grazie di aver salvato il mio tempo –