How to return to default style on EditText if I apply a background?(如果我应用背景,如何在 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 上返回默认样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果我应用背景,如何在 EditText 上返回默认样式
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
