这是我创建 EditText 时的默认方式:
这是我改变它的背景后的样子:
要将背景更改为上图,我将以下 EditText 属性添加到我的 XML 文件中的 EditText:
android:background="@android:drawable/edit_text"
我想做的是做我上面在 Activity 类中所做的事情(让用户可以在经典样式和透明/新样式之间进行选择)。
所以像这样 - 我需要人们填写 /*...*/
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 样式呢?
最佳答案
我最终尝试在打开布局时只保存原始 Drawable
,然后在需要时以这种方式设置背景资源。像这样:
在我的 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);
}
我仍然鼓励更多的答案,因为我不太喜欢这个解决方案。
来源:
How to return to default style on EditText if I apply a background?
关于android - 以编程方式将 EditText 的背景资源设置为透明字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21791760/