【问题标题】:Custom edit text with different view on different state in android在android中的不同状态下自定义编辑文本具有不同的视图
【发布时间】:2015-08-13 08:44:45
【问题描述】:
我的编辑文本现在看起来像这样(就像它的背景一样)
http://prntscr.com/843323
这是代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
</selector>
当我点击它来写东西时,如何将下划线设置为这个编辑文本(例如白色)。http://prntscr.com/8434s7
【问题讨论】:
标签:
android
android-edittext
android-selector
android-shape
【解决方案1】:
试试这个:
how to change focus color of EditText in Android
或者这个:
<style name="Theme.MyTheme.EditText" parent="AppTheme">
<item name="colorControlNormal">#ff6600</item>
<item name="colorControlActivated">#ff6600</item>
</style>
<EditText
style="@style/TextAppearance.AppCompat.Display1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/materialDesignEditText"
android:ems="10"
android:hint="Normal EditText"
android:padding="5dp"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="@android:color/secondary_text_dark"
android:textSize="15dp"
android:theme="@style/Theme.MyTheme.EditText" />
【解决方案2】:
创建 textfield_default
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="Color Hash Code"
android:endColor="@color/background_material_light"
android:angle="90" />
</shape>
在drawable中创建edit_text.xml
<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>
<EditText
style="@drawable/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/materialDesignEditText"
android:ems="10"
android:hint="Text here"
android:padding="5dp"
android:singleLine="true"
android:textColor="#000000"
android:textColorHint="@android:color/secondary_text_dark"
android:textSize="15dp" />