我需要设计EditText,使其基线显示在下图中,当它获得焦点时,它将变为其他颜色!!
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9tNk9hOS5wbmcifQ==” alt =“在此处输入图像描述”>
我正在使用以下内容。
<EditText
android:id="@+id/mobilenumber"
android:drawableLeft="@drawable/mobile"
android:drawablePadding="15dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:background="@drawable/edt_bg"
android:singleLine="true"
android:textColorHint="#FFFFFF"
android:hint="@string/mobilenumber"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:padding="5dp"
android:imeOptions="actionNext"
android:maxLength="10"
android:textColor="#ffffff"
android:textCursorDrawable="@null"
/>
所以,请指导我如何处理。
您可以为EditText添加主题
<style name="EditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/primary</item>
<item name="colorControlActivated">@color/primary</item>
<item name="colorControlHighlight">@color/primary</item>
</style>
而且您可以在EditText中使用
<EditText
android:id="@+id/edtCode"
android:layout_width="match_parent"
android:layout_height="40dp"
.......................
android:theme="@style/EditTextTheme">
</EditText>
以您的editText样式使用它
<item name="backgroundTint">@color/focus_tint_list</item>
focus_tint_list.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="?attr/colorAccent"/>
<item android:state_focused="false" android:color="#999999"/>
</selector>
尝试使用Holo颜色生成器自定义颜色。
您可以创建选择器并将其设置为EditText
的背景
res / drawable / edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/textfield_default" />
<item
android:state_window_focused="false"
android:state_enabled="false"
android:drawable="@drawable/textfield_disabled" />
<item
android:state_pressed="true"
android:drawable="@drawable/textfield_pressed" />
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="@drawable/textfield_selected" />
<item
android:state_enabled="true"
android:drawable="@drawable/textfield_default" />
<item
android:state_focused="true"
android:drawable="@drawable/textfield_disabled_selected" />
<item
android:drawable="@drawable/textfield_disabled" />
</selector>
layout:
<EditText
android:id="@+id/mobilenumber"
....
android:background="@drawable/edit_text" />
我需要同样的东西,并且通过以下方式解决了它:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_window_focused="true">
<shape>
<stroke android:color="@color/blue_500" android:width="2dp" />
<corners android:radius="45dp"/>
</shape>
</item>
<item android:state_focused="false" android:state_window_focused="false">
<shape>
<stroke android:color="@color/grey_600" android:width="2dp"/>
<corners android:radius="45dp"/>
</shape>
</item>
</selector>
为了在焦点对准时更改EditText的基线,您只需转到项目中的colors.xml文件并更改“ colorAccent”值。
res / values / colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="colorAccent">#DESIRED_COLOR</color>
</resources>`
您可以在可绘制目录中创建一个带有项目android:state_focused =“ true”以及其他默认颜色的项目的selector.xml文件像这样
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/border_blue" />
<item android:drawable="@drawable/border" />
</selector>
并且使用此选择器作为editText的背景,例如这样
<EditText
android:background="@drawable/selector"
/>