以编程方式将EditText的背景资源设置为透明字段

时间:2014-02-15 00:18:53

标签: android android-edittext android-resources android-drawable

这是EditText创建时的默认方式:

这是我改变背景后的样子:

enter image description here

要将背景更改为上图,我将以下EditText属性添加到我的XML文件中的EditText:

android:background="@android:drawable/edit_text"

我想做的是在我的活动课上做我上面做过的事情(给用户一个选择经典风格和透明/新风格的选项)。

所以这样 - 我需要人们填写/*...*/

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样式?

2 个答案:

答案 0 :(得分:10)

我最后试图在打开布局时保存原始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?

setBackground vs setBackgroundDrawable (Android)

答案 1 :(得分:1)

默认的全息EditText的背景是:edit_text_holo_dark。那是黑暗的版本。还有与之相对应的轻型版本:edit_text_holo_light

<强> edit_text_holo_dark.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_multiline="true" android:state_window_focused="false" android:state_enabled="true"  android:drawable="@drawable/textfield_multiline_default_holo_dark" />
    <item android:state_multiline="true" android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_multiline_disabled_holo_dark" />
    <item android:state_multiline="true" android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_multiline_activated_holo_dark" />
    <item android:state_multiline="true" android:state_enabled="true" android:state_activated="true" android:drawable="@drawable/textfield_multiline_focused_holo_dark" />
    <item android:state_multiline="true" android:state_enabled="true" android:drawable="@drawable/textfield_multiline_default_holo_dark" />
    <item android:state_multiline="true" android:state_focused="true" android:drawable="@drawable/textfield_multiline_disabled_focused_holo_dark" />
    <item android:state_multiline="true" android:drawable="@drawable/textfield_multiline_disabled_holo_dark" />

    <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/textfield_default_holo_dark" />
    <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_disabled_holo_dark" />
    <item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_activated_holo_dark" />
    <item android:state_enabled="true" android:state_activated="true" android:drawable="@drawable/textfield_focused_holo_dark" />
    <item android:state_enabled="true" android:drawable="@drawable/textfield_default_holo_dark" />
    <item android:state_focused="true" android:drawable="@drawable/textfield_disabled_focused_holo_dark" />
    <item android:drawable="@drawable/textfield_disabled_holo_dark" />
</selector>

请注意,这些全息元素仅适用于Android HC +。