2012-06-13 38 views
2

我有一个简单的EditText字段,显示登录页面上用户的电话号码。初次登录后,电话号码字段被禁用。在某些设备上禁用时,EditText无法读取

这在几乎所有的设备看起来很大(这个截图是从三星Galaxy S):
enter image description here

然而,在我的LG Nitro在残疾人的EditText字段中的文本是不可读(我可以能看到白色的文字,如果我放大某个高分辨率截图):
enter image description here

我删除从EditText上,并出现同样的问题我所有的自定义样式规则,所以我认为这仅仅是一个坏的选择系统默认列或者打电话。

问题1:有人可以确认我的诊断是否正确吗?

我可以使文本可读的唯一方法是在代码中设置文本深灰色:

if (fieldDisabled) 
{ 
    // Some devices use their own default style for a disabled text field, 
    // which makes it impossible to read its text, e.g. the LG Nitro. 
    // 
    // The workaround is to override the text color here. 
    mPhoneNumber.setTextColor(Color.DKGRAY); 
} 

之后的文本很容易上的所有设备(包括LG硝基)阅读:
enter image description here

我将自定义样式设置为使用@color/black代替现有颜色,但文本仍显示为白色。

问题2:有没有更好的解决方法我可以使用?

我的LG Nitro是运行OS 2.3.5的型号LG-P930。

我的XML

下面是我使用的XML的片段。

RES /布局/ myscreen.xml:

<EditText 
    ... 
    android:textAppearance="@style/MyStyle"> 
</EditText> 

RES /值/ styles.xml:

<style name="MyStyle"> 
    <item name="android:textSize">14dp</item> 
    <item name="android:textColor">@color/blue</item> 
</style> 

RES /值/ colors.xml:

<color name="white">#ffffffff</color> 
<color name="blue">#ff0000ff</color> 
<color name="black">#ff000000</color> 
+2

而不是禁用的EditText后,您可以进行的EditText可聚焦假的初始状态。所以它不会得到重点,它会看起来很完美 – Sumant

+0

这是一个很好的建议,并做了我想要的很多东西。仅供参考,我需要调用setFocusableInTouchMode(false)以及setFocusable(false)才能使其工作。然而,缺点是它弄乱了我的焦点逻辑(我可以修复),更重要的是,文本字段现在看起来像一个启用的文本字段而不是禁用的字段。 –

+0

是的,我认为通过你的问题解决.... :) – Sumant

回答

6

我想出了如何改变EditText文本的颜色。

使用android:textAppearance确实不是似乎允许您更改EditText中文本的颜色(它可以让您更改文本大小)。

一种替代方法是使用style属性而不是android:textAppearance,因为这将应用文本颜色更改,例如,

style="@style/MyStyle" 

但是,我认为最好的解决方案是使用ColorStateList。以下是我的解决方案。

RES /布局/ myscreen.xml(仍需textAppearance控制文字大小):

<EditText 
    ... 
    android:textColor="@color/edittext" 
    android:textAppearance="@style/MyStyle"> 
</EditText> 

RES /颜色/ edittext.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="true" android:color="@color/black" /> 
    <item android:state_enabled="false" android:color="@color/grey" /> 
</selector> 

RES /价值/风格。 XML(即让myStyle的唯一定义文本大小,不变色):

<style name="MyStyle"> 
    <item name="android:textSize">14dp</item> 
</style> 
相关问题