3
如何在给它一个自定义颜色后重置EditText小部件的默认背景?在自定义背景后重置EditText的背景
如何在给它一个自定义颜色后重置EditText小部件的默认背景?在自定义背景后重置EditText的背景
我必须为我写的应用程序执行此操作。在本示例中,我检查EditText
是否为空,如果它为空,则创建一个TextWatcher
。检查出来:
// highlight any unfilled boxes
final EditText text = (EditText) activity.findViewById(id);
String data = text.getText().toString();
if (data.length() == 0) //
{
final Drawable oldBackground = activity.findViewById(id).getBackground();
text.setBackgroundColor(Color.YELLOW);
text.addTextChangedListener(new TextWatcher() //
{
@Override
public void afterTextChanged(Editable s) //
{
text.setBackgroundDrawable(oldBackground);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{ }
});
}
希望这会有所帮助!
谢谢,这个工作。我在编辑之前使用getBackground获取参考,然后将背景设置为该参考值以重置。 – 2012-01-17 18:20:38
@StefanDunn优秀!很高兴我能帮上忙。 – Codeman 2012-01-17 18:26:19