我正在使用TextinputLayout和TetinputEditText来获取浮动提示.现在我想更改编辑文本的提示和底线的颜色.我想让用户点击编辑文本时进行更改.
所以我尝试更改编辑文本的onClickListener颜色.但我没有发现颜色有任何变化.
xml布局:
<linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="@androID:color/white" androID:orIEntation="vertical"> <linearLayout androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <androID.support.design.Widget.TextinputLayout androID:ID="@+ID/input_layout_item_name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:layout_margintop="20dp" androID:paddingtop="05dp"> <androID.support.design.Widget.TextinputEditText androID:layout_wIDth="240dp" androID:layout_height="45dp" androID:ID="@+ID/editTextItemname" androID:layout_gravity="center_horizontal" androID:hint="@string/item_name" androID:textSize="12sp" /> </androID.support.design.Widget.TextinputLayout> </linearLayout>
java代码:
public voID setUpUI(){ edt_Item_name = (TextinputEditText) findVIEwByID(R.ID.editTextItemname); edt_Item_quantity = (TextinputEditText)findVIEwByID(R.ID.editTextItemQuantity); edt_Item_Unit = (TextinputEditText)findVIEwByID(R.ID.editTextItemunit); textinput_Item_name = (TextinputLayout)findVIEwByID(R.ID.input_layout_item_name); textinput_Item_quantity = (TextinputLayout)findVIEwByID(R.ID.input_layout_item_quantity); textinput_Item_Unit = (TextinputLayout)findVIEwByID(R.ID.input_layout_item_unit); edt_Item_name.getBackground().mutate().setcolorFilter(ContextCompat.getcolor(this, R.color.edtcolor), PorterDuff.Mode.SRC_Atop); edt_Item_name.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { edt_Item_name.setHintTextcolor(ContextCompat.getcolor(SearchActivity.this, R.color.edtcolor)); edt_Item_name.getBackground().mutate().setcolorFilter(ContextCompat.getcolor(SearchActivity.this, R.color.edtcolor), PorterDuff.Mode.SRC_Atop); } }); }}
输出:
我怎样才能做到这一点?
解决方法:
在colors.xml文件中定义颜色
<item name="colorControlnormal">#c5c5c5</item><item name="colorControlActivated">@color/accent</item><item name="colorControlHighlight">@color/accent</item>
并在styles.xml中创建样式
<style name="TextAppearence.App.TextinputLayout" parent="@androID:style/TextAppearance"> <item name="androID:textcolor">@color/main_color</item></style>
并应用于TextinputLayout
app:hintTextAppearance="@style/TextAppearence.App.TextinputLayout" androID:textcolorHint="#0072BA"
将您的代码更改为:
<linearLayout androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <androID.support.design.Widget.TextinputLayout androID:ID="@+ID/input_layout_item_name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:layout_margintop="20dp" androID:paddingtop="05dp" app:hintTextAppearance="@style/TextAppearence.App.TextinputLayout" androID:textcolorHint="#0072BA"> <androID.support.design.Widget.TextinputEditText androID:layout_wIDth="240dp" androID:layout_height="45dp" androID:ID="@+ID/editTextItemname" androID:layout_gravity="center_horizontal" androID:hint="@string/item_name" androID:textSize="12sp" /> </androID.support.design.Widget.TextinputLayout> </linearLayout>
总结 以上是内存溢出为你收集整理的如何更改TextInputEditText的底线颜色和提示颜色?全部内容,希望文章能够帮你解决如何更改TextInputEditText的底线颜色和提示颜色?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)