当我使用 setEnabled(false)
或通过 xml 文件 android:enabled="false"
禁用 EditText 时,背景颜色将为灰色:
如果我将背景更改为白色 android:background="@color/white"
,我将失去边框:
所以我的问题是,如何禁用 EditText(我不希望在长按它时弹出任何对话框)并保持边框不变?
最佳答案
最好的办法是创建自己的状态列表可绘制对象,并将禁用背景指定为与启用状态相同。您需要创建自己的 9-Patch 镜像,或者您也可以从 Android 开源中提取资源文件。
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
这是我用于 EditText 背景的示例:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:state_focused="true"
android:drawable="@drawable/edittext_bg_selected" />
<!-- Inactive tab -->
<item android:state_focused="false"
android:drawable="@drawable/edittext_bg_unselected" />
<item android:state_enabled="false"
android:drawable="@drawable/edittext_bg_unselected" />
<!-- Add all of your other states -->
</selector>
您只需要弄乱不同的状态即可创建适合您的状态。
关于java - 如何禁用 EditText 并保持背景颜色为白色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9367045/