EditText上的编程设置背景资源为透明场 [英] Programmatically Set Background Resource of EditText to transparent field
问题描述
这是默认的方式,当我创建一个一个EditText样子:
This is the default way an EditText looks when I create one:
这是什么样子后,我改变了它的背景:
This is what it looks like after I change the background of it:
要改变背景的图像上面,我下面的EditText属性添加到我的EditText在我的XML文件:
To change the background to the image above, I add the following EditText property to my EditText in my XML file:
android:background="@android:drawable/edit_text"
我想要做的是做什么,我已经在我的活动类上面做(给用户一个选择经典的款式和透明/新风格之间进行选择)。
What I want to do is do what I've done above in my activity class (giving the user an option to select between the classic style and the transparent/new style).
所以这样的 - 我需要人来填补 /*...*/
So like this - I need people to fill in the /*...*/
if (classicTextbox()) { // the blocky white one
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(android.R.drawable.edit_text);
} else { // the new transparent one
editText.setTextColor(Color.WHITE);
editText.setBackgroundResource(/*...*/);
}
那么,如何改回为新的,透明的EditText风格?
So how do I change it back to the new, transparent EditText style?
推荐答案
最后我想只保存原始可绘制
时的布局被打开,然后设置背景的资源,这样,每当我想要的。像这样的:
I ended up trying to just save the original Drawable
when the layout is opened and then set the background resource that way whenever I want. Like this:
在我的的onCreate
方法:
Drawable originalDrawable = editText.getBackground();
和再设置它:
// need to use .setBackground and .setBackgroundDrawable depending on the
// android version because .setBackgroundDrawable is depreciated
int sdk = android.os.Build.VERSION.SDK_INT;
int jellyBean = android.os.Build.VERSION_CODES.JELLY_BEAN;
if(sdk < jellyBean) {
editText.setBackgroundDrawable(originalDrawable);
} else {
editText.setBackground(originalDrawable);
}
我还是鼓励更多的答案,因为我不太喜欢这样的解决方案。
I still encourage more answers because I don't quite like this solution.
来源:
<一个href=\"http://stackoverflow.com/questions/6912237/how-to-return-to-default-style-on-edittext-if-i-apply-a-background\">How返回到默认样式上的EditText如果我申请一个背景?
<一个href=\"http://stackoverflow.com/questions/11947603/setbackground-vs-setbackgrounddrawable-android\">setBackground VS setBackgroundDrawable(Android版)
这篇关于EditText上的编程设置背景资源为透明场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!