TextView
の1語のテキストの色をActivity
から変更する方法を探しています。私は赤にnext
テキストの色を変更する方法をTextViewの1語のテキストの色を変更します
String first = "This word is ";
String next = "red"
TextView t = (TextView) findViewById(R.id.textbox);
t.setText(first + next);
:これで例えば
、?
TextView
の1語のテキストの色をActivity
から変更する方法を探しています。私は赤にnext
テキストの色を変更する方法をTextViewの1語のテキストの色を変更します
String first = "This word is ";
String next = "red"
TextView t = (TextView) findViewById(R.id.textbox);
t.setText(first + next);
:これで例えば
、?
私が知っている最も簡単な方法は、単にhtmlを使うことです。
String first = "This word is ";
String next = "<font color='#EE0000'>red</font>";
t.setText(Html.fromHtml(first + next));
しかし、これはあなたが面倒を引き起こす可能性の色を変更したい場合(IF?)のTextViewを再構築するためにあなたを必要とします。
t.setText(first + next, BufferType.SPANNABLE);
Spannable s = (Spannable)t.getText();
int start = first.length();
int end = start + next.length();
s.setSpan(new ForegroundColorSpan(0xFFFF0000), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
あなたはspannableを使用する必要があります。これにより、テキストのサイズを増やしたり、太字にするなどしても画像を入れたりすることができます。
あなたはTextView
テキスト(大文字小文字を区別しない)内の特定のString
のすべてのインスタンスの状態を変更したい場合は、StringBuilder
sであり、このようなSpannableString
使用することができます。
StringBuilder textBuilder = new StringBuilder(myTextView.getText().toString());
StringBuilder searchedTextBuilder = new StringBuilder((mySearchedString));
SpannableString spannableString = new SpannableString(myTextView.getText().toString());
int counter = 0;
int index = 0;
for (int i = 0;i < textBuilder.length() - mySearchedString.length() - 1;i++)
{
counter = 0;
if (Character.toLowerCase(textBuilder.charAt(i)) == Character.toLowerCase(searchedTextBuilder.charAt(index)))
{
counter++;
index++;
for (int j = 1,z = i + 1;j < mySearchedString.length() - 1;j++,z++)
{
if (Character.toLowerCase(textBuilder .charAt(z)) == Character.toLowerCase(searchedTextBuilder .charAt(index)))
{
counter++;
index++;
}
else
{
index++;
if (index % mySearchedString.length() == 0)
{
index = 0;
}
break;
}
}
if (counter == mySearchedString.length() - 1) // A match
{
spannableString.setSpan(new ForegroundColorSpan(Color.RED), i,
i + mySearchedString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Do the change you want(In this case changing the fore ground color to red)
index = 0;
continue;
}
else
{
index = 0;
continue;
}
}
}
myTextView.setText(spannableString);
}
TextView
テキスト全体をStringBuilder
の内部に格納します。StringBuilder
に格納します。SpannableString
TextView
テキストはTextView
テキスト内のすべてのString
のインスタンスを検索し、到達したときにそれらを変更するには、簡単な操作を行います。TextView
のテキスト値をSpannableString
に設定します。このような使用SpannableStringBuilder:
SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString str1= new SpannableString("Text1");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);
SpannableString str2= new SpannableString(appMode.toString());
str2.setSpan(new ForegroundColorSpan(Color.GREEN), 0, str2.length(), 0);
builder.append(str2);
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText(builder, TextView.BufferType.SPANNABLE);
'新しいForegroundColorSpan(Color)'を置き換えて、テキストを元のデフォルトの色に保つことができるものはありますか? – cjnash
の可能性のある重複した[それはTextViewの内側に複数のスタイルを持っていることは可能ですか?](http://stackoverflow.com/questions/1529068/is-it-複数のスタイルを持つことが可能なテキストビュー) – blahdiblah
http://stackoverflow.com/questions/9754076/which-html-tags-are-supported-by-android-textview/35271588#35271588 –