如果我应用背景,如何在 EditText 上返回默认样式? [英] How to return to default style on EditText if I apply a background?

查看:46
本文介绍了如果我应用背景,如何在 EditText 上返回默认样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了一个我不知道如何解决的问题.它看起来很傻,但我找不到解决方法.

I just encountered with a problem that I don't know how to solve. It looks silly but I cannot find a way to fix it.

我在 android 中有一个带有多个 EditText 的表单,当您提交表单时,它会检查那些 EditText,如果它们有错误的数据,我会将 EditText 更改为红色边框(应用 Ninepatch 图像).

I have a form in android with several EditText and when you submit the form it checks those EditText and if they have wrong data I change the EditText to red border (applying a ninepatch image).

myEditText.setBackgroundResource(R.drawable.edittext_red);

问题是,当用户更改EditText时,我想恢复android的默认样式,但我不知道该怎么做.如果我这样做:

The problem is, I want to restore android's default style when the user change the EditText, but I don't know how to do it. if I do:

myEditText.setBackgroundResource(0);

它只会删除所有内容,这不是我想要的.

It will just remove everything and that's not what I want.

谢谢!

推荐答案

首先,在使用自己的 Ninepatch 调用 setBackgroundResource 之前,先存储 EditText 的原始背景,如下所示:

First, before you call setBackgroundResource with your own ninepatch, store the original background of the EditText, like this:

Drawable originalDrawable = myEditText.getBackground();

然后,如果用户输入了错误的数据,你可以设置你的红色边框 Ninepatch:

Then, if the user entered the wrong data, you can set your red border ninepatch:

myEditText.setBackgroundResource(R.drawable.edittext_red);

然后,当你想恢复 EditText 的外观时,使用存储的 drawable:

And later, when you want to restore the look of the EditText, use the stored drawable:

myEditText.setBackgroundDrawable(originalDrawable);

或者您可以像这样直接引用默认的 Android EditText 背景:

Alternatively you can reference the default Android EditText background directly like this:

myEditText.setBackgroundResource(android.R.drawable.edit_text);

androiddrawables,您可以看到不同的可绘制对象如何查找不同的 Android 版本,并获取它们的资源标识符.此方法适用于所有可绘制对象,而不仅仅是 EditText

At androiddrawables you can see how the different drawables look for the different versions of Android, and get their resource identifier. This method should work for all drawables, not just EditText

这些(和其他 android 资源)也可以在您自己的系统上找到,在 android-sdk 文件夹中

These (and other android resources) can also be found on your own system, in the android-sdk folder in

/android-sdk/platforms/android-<API级别>/data/res/

< path to android-sdk folder>/android-sdk/platforms/android-< API level>/data/res/

这篇关于如果我应用背景,如何在 EditText 上返回默认样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆