如何禁用EditText并保持背景颜色为白色?

时间:2012-02-20 19:16:44

标签: java android android-edittext background-color

当我使用setEnabled(false)或xml文件android:enabled="false"禁用EditText时,背景颜色将为灰色:

enter image description here

如果我用白色android:background="@color/white"更改背景,我将失去边框:

enter image description here

所以我的问题是,如何禁用EditText(我不希望任何对话框长时间点击它)并保持边框原样?

3 个答案:

答案 0 :(得分:3)

您最好的选择是创建自己的状态列表Drawable,并指定禁用的背景与启用状态相同。您需要创建自己的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>

你只需要弄乱不同的状态来创造适合你的东西。

答案 1 :(得分:1)

我找到了办法:

android:editable="false"
android:cursorVisible="false"
editText.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        return true;
    }
});

答案 2 :(得分:0)

尝试使用TextView代替android:background="#FFFFFF"

您也可以尝试android:focusable="false"

相关问题