2012-02-20 85 views
1

当我setEnabled(false)或通过XML文件android:enabled="false"禁用的EditText,背景颜色将是灰色的:如何禁用EditText并保持背景颜色为白色?

enter image description here

,如果我改变了白色android:background="@color/white"的背景下,我会失去边界:

enter image description here

所以我的问题是,如何禁用的EditText(我不希望在其上长击将筋疲力尽的任何对话),并保持边境,因为它是什么?

+0

这个答案为我工作(当这些都不一样): http://stackoverflow.com/a/7696282/ 1686442 – 2013-04-04 15:14:22

回答

1

我找到了一种方法来做到这一点:

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

EditText没有“setEditable”! – AVEbrahimi 2012-03-17 07:51:50

+0

@Avebrahimi是的,谢谢你通知我,我更新了答案! – 2012-03-17 15:21:17

0

尝试使用TextView而不是android:background="#FFFFFF"。您也可以尝试android:focusable="false"

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> 

你只需要使用不同的状态一塌糊涂创建你的作品。