問題描述
當(dāng)用戶在 EditText 中輸入信息,并移動(dòng)到下一個(gè) EditText 時(shí),信息會(huì)高亮顯示,如下所示:
When a user enters information in an EditText, and moves to the next EditText, the information is highlighted as shown below:
代碼:
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setBackgroundColor(Color.WHITE);
((EditText) v).setTextColor(Color.BLACK);
} else {
//v.setBackgroundColor(Color.LTGRAY); //also works like this
((EditText) v).setBackgroundColor(Color.LTGRAY);
((EditText) v).setTextColor(Color.BLACK);
}
}
});
在 onCreate
方法中調(diào)用如下:
Which is called in the onCreate
method like this:
edittext = (EditText) findViewById(R.id.editText1);
但是,如果背景顏色只應(yīng)用于文本本身而不是視圖,那就更好了,就像這樣(來自 gmail 應(yīng)用程序):
However, It would be much better if the background color only applied to the text itself, rather than the view, like this (from the gmail app):
是否有人對(duì)如何將背景顏色僅應(yīng)用于文本(而不是整個(gè) EditText 視圖)有任何建議?
Does anybody have any suggestions on how to apply the background color to the text only (not the whole EditText view) as above?
謝謝.
推薦答案
您可以使用 BackgroundColorSpan
來實(shí)現(xiàn)您想要的.您可以在此處找到更多信息:
You can achieve what you want by using a BackgroundColorSpan
. You can find more information here:
http://developer.android.com/reference/android/text/style/BackgroundColorSpan.html
要使用 span,您需要構(gòu)建一個(gè) SpannableString
,您可以使用 SpannableStringBuilder
:
To use spans you need to build a SpannableString
which you can do using a SpannableStringBuilder
:
http://developer.android.com/reference/android/text/SpannableStringBuilder.html一個(gè)>
這篇關(guān)于EditText - 更改文本的背景顏色(只有文本 - 不是整個(gè)視圖)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!