如何在为其提供自定义颜色后重置EditText小部件的默认背景?
答案 0 :(得分:2)
我必须为我写的应用程序执行此操作。在此示例中,我检查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)
{ }
});
}
希望这有帮助!