2

如何设置浮动标签文本颜色的edittext时,它不是不同于edittext提示颜色的焦点?如何设置edittext的浮动标签文本颜色,当它不是焦点不同于edittext提示颜色?

我将提示颜色设置为最初的灰色。当用户将注意力放在edittext上时,它会变为红色。问题是在输入某些文本后删除edittext的焦点,然后将edittext的标签文本颜色浮动更改为grey.I需要它只保留红色。只有在edittext中没有文本时才应该提示颜色,即使edittext不在焦点中,浮动标签颜色也应该始终为红色。

下面的代码我是using-

<android.support.design.widget.TextInputLayout 
         android:id="@+id/amountLayout" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout" 
         android:paddingTop="@dimen/dp1"> 

         <android.support.design.widget.TextInputEditText 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:hint="@string/amount" 
          android:textColor="@color/red" 
          android:inputType="number"/> 

</android.support.design.widget.TextInputLayout> 

<style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance"> 
     <item name="android:textColor">@color/red</item> 
     <item name="android:textSize">@dimen/sp8</item> 
    </style> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorControlNormal">@color/red</item> 
     <item name="colorControlActivated">@color/red</item> 
     <item name="colorControlHighlight">@color/red</item> 
    <item name="android:textColorHint">@color/grey</item> 
</style> 

回答

1

你可以通过内部EDITTEXT

android:textColorHighlight="@android:color/white" 

插入该属性改变的浮动提示的颜色,或者如果你想改变颜色editText(不是浮动的)你可以添加这个在你的主题

<item name="android:textColorHint">@android:color/white</item> 
+0

的问题是,当EditText上不上集中foating edittext的标签文本颜色更改为提示颜色..因此,无论我在android中指定了什么颜色:textColorHint,一旦edittext不在焦点中,浮动标签颜色将更改为该颜色 –

1

我也有这个专业版瑕疵,但我解决了它,这是我的解决方案,可以帮助你。

仅在您的TextInputLayout中添加以下,而不是在TextInputEditText中。

android:textColorHint="@color/yourLabelColorWhenNotFocus" 

如果您同时TextInputEditText和TextInputLayout该属性,或者只是在TextInputEditText,就不能很好地工作。

0

我无法找到一个方法来做到这一点,从阅读的TextInputLayout源,原来是因为没有办法修改私有成员mDefaultTextColor,这是TextInputLayout什么,这是不可能的编程将提示颜色更改为视图未聚焦时。设置此的唯一方法是通过xml:

android:textColorHint="@color/red" 

这显然是不够的,因为我们希望能够动态地设置它。

因此,我通过一对扩展函数为它编写了访问器

/** 
* We need these accessors because this field is private and programmatically unstyleable, 
* and we want to change the default (both focused and unfocused) hint text color. 
* 
* This is made safe by simply defaulting to null at any point there could be a problem. 
*/ 
private fun TextInputLayout.getDefaultTextColor(): ColorStateList? { 
    javaClass.getDeclaredField("mDefaultTextColor").let { field -> 
     field.isAccessible = true 
     return field.get(this) as? ColorStateList? 
    } 
} 

private fun TextInputLayout.setDefaultTextColor(color: ColorStateList) { 
    javaClass.getDeclaredField("mDefaultTextColor").let { field -> 
     field.isAccessible = true 
     field.set(this, color) 
    } 
} 

用法:

//Get the original color 
normalTextColor = text_input_layout.getDefaultTextColor() 

//Change the color to another color 
text_input_layout.setDefaultTextColor(ContextCompat.getColorStateList(context, R.color.red)) 
text_input_layout.setHintTextAppearance(R.style.TextAppearance_Error) 

//Change the color back to the default 
//(the TextInputLayout likes to use the inner TextInputEditText's hint color state list as a fallback, so we will too) 
text_input_layout.setDefaultTextColor(normalTextColor ?: inner_text.hintTextColors) 
text_input_layout.setHintTextAppearance(R.style.TextAppearance_Design_Hint) 

你会注意到上述引用一个​​风格,它的定义很简单:

<style name="TextAppearance.Error" parent="TextAppearance.Design.Hint"> 
    <item name="android:textColor">@color/red</item> 
</style> 
相关问题