如何返回到默认的样式上的EditText,如果我申请一个背景? [英] How to return to default style on EditText if I apply a background?

查看:223
本文介绍了如何返回到默认的样式上的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.

我有一个形式,机器人与几个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.

谢谢!

推荐答案

首先,你打电话setBackgroundResource用自己的ninepatch之前,存储的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的外观,使用存储绘制:

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

myEditText.setBackgroundDrawable(originalDrawable);

或者您可以直接引用默认的Andr​​oid的EditText的背景是这样的:

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

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

androiddrawables 你可以看到不同的可绘制如何看待不同版本的Andr​​oid,并得到他们的资源标识符。此方法应适用于所有可绘,不只是的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文件夹> / Android的SDK /平台/ android-< API级别> /数据/ RES /

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

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

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