問題描述
我剛剛遇到了一個我不知道如何解決的問題.它看起來很傻,但我找不到解決方法.
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,如果它們有錯誤的數(shù)據(jù),我會將 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);
它只會刪除所有內(nèi)容,這不是我想要的.
It will just remove everything and that's not what I want.
謝謝!
推薦答案
首先,在使用自己的 Ninepatch 調(diào)用 setBackgroundResource 之前,先存儲 EditText 的原始背景,如下所示:
First, before you call setBackgroundResource with your own ninepatch, store the original background of the EditText, like this:
Drawable originalDrawable = myEditText.getBackground();
然后,如果用戶輸入了錯誤的數(shù)據(jù),你可以設置你的紅色邊框 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 資源)也可以在您自己的系統(tǒng)上找到,在 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 上返回默認樣式?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!