1

我有一个我验证的编辑文本。如果输入的数据与格式不对应,我将背景设置为红色,当它对应时,我将其设置回浅灰色,但矩形消失了。我想知道当输入的数据格式正确时,是否可以将其属性重置为其原始值。这就是我现在正在做的

EditText name = (EditText) findViewById(R.id.Name);
if (name.getText().length() < 1)
{
    error = true;
    unit.setBackgroundColor(Color.RED);
}
else
{
    //instead of this line reset edittext properties
    unit.setBackgroundColor(Color.LTGRAY);
}
4

5 回答 5

0

你可以试试:

editText.setText("");
于 2013-05-30T07:58:32.070 回答
0

You are changing the EditText background so you are overriding android's default background.
you need to manually change it to some other background.
you can save it by Using the getBackground() method in EditText.

于 2013-05-30T08:05:31.533 回答
0

我建议使用该setError()方法,它会在 上方显示一条错误消息,EditText并在内容EditText更改时自动将其删除。这是向用户显示验证失败的标准方式。

于 2013-05-30T08:01:01.027 回答
0

您可以保存editText之前的状态进行更改,并在需要时恢复:

private Drawable originalDrawable;

EditText name = (EditText) findViewById(R.id.Name);
if (name.getText().length() < 1) {
    if (originalDrawable == null) {
        originalDrawable = unit.getBackground();
    }
    error = true;
    unit.setBackgroundColor(Color.RED);
}
else {
    //reset editText 
    text.setBackgroundDrawable(originalDrawable);
}
于 2016-12-13T15:46:41.413 回答
0

您可以改用 PorterDuff - 它有一个明确的方法:http: //developer.android.com/reference/android/graphics/PorterDuff.Mode.html

要设置颜色:

name.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);

要删除颜色:

name.getBackground().clearColorFilter();
于 2015-08-05T14:12:29.070 回答