Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

android - How to return to default style on EditText if I apply a background?

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.

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);

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.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Drawable originalDrawable = myEditText.getBackground();

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

myEditText.setBackgroundResource(R.drawable.edittext_red);

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

myEditText.setBackgroundDrawable(originalDrawable);

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

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...