无论 TextInputEditText 中的焦点状态如何,如何显示该行?
How to display the line regardless of the focused state in a TextInputEditText?
发生了什么事:当我点击 inputName
时,上面出现了一行
我在尝试什么:无论焦点是否存在都显示该行
代码:
<android.support.design.widget.TextInputEditText
android:id="@+id/inputName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:singleLine="true"
android:textColor="@color/warm_grey"
android:hint="Enter Mobile Number"
android:layout_weight="1"/>
图片:
我只想为聚焦和未聚焦状态提供两种颜色
在您的 AppTheme
中尝试此属性,或者只创建 AnotherTheme 并在您的 EditText 中使用。
<item name="colorControlNormal">Color code for default</item>
<item name="colorControlActivated">Color code for focused one</item>
<item name="colorControlHighlight">Color code for focused one</item>
可能重复 Changing EditText bottom line color with appcompat v7
注意:- 如果您使用 AppCompat v22
支持库,您可以在 EditText 中指定主题,例如:android:theme="@style/Theme.App.Base
。这将确保样式不会同时影响布局中您不想更改的其他视图。
您需要为此
使用 StateListDrawable
- StateListDrawable 是 XML 中定义的可绘制对象,它使用多个不同的图像来表示相同的图形,具体取决于对象的状态。
示例代码
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint Text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edt_bg"
android:visibility="visible" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint Text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edt_bg"
android:visibility="visible" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
@drawable/edt_bg
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/edt_bg_selected" android:state_focused="true" />
<item android:drawable="@drawable/edt_bg_normal" android:state_focused="false" />
</selector>
@drawable/edt_bg_selected
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#0fe4e4" />
<solid android:color="#00ffffff" />
</shape>
</item>
</layer-list>
@drawable/edt_bg_normal
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#000" />
<solid android:color="#00ffffff" />
</shape>
</item>
</layer-list>
输出
注意
根据您的要求使用android:width="2dp"
和android:color="#000"
设置Thickness宽度和线条颜色你的 TextInputEditText
这个小功能或许能帮到你
fun hintRemover(textInputLayout: TextInputLayout, textInputEditText: TextInputEditText, text: String) {
textInputEditText.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
textInputLayout.hint = null
}
else {
textInputLayout.hint = text
}
}
}
发生了什么事:当我点击 inputName
时,上面出现了一行
我在尝试什么:无论焦点是否存在都显示该行
代码:
<android.support.design.widget.TextInputEditText
android:id="@+id/inputName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:singleLine="true"
android:textColor="@color/warm_grey"
android:hint="Enter Mobile Number"
android:layout_weight="1"/>
图片:
我只想为聚焦和未聚焦状态提供两种颜色
在您的 AppTheme
中尝试此属性,或者只创建 AnotherTheme 并在您的 EditText 中使用。
<item name="colorControlNormal">Color code for default</item>
<item name="colorControlActivated">Color code for focused one</item>
<item name="colorControlHighlight">Color code for focused one</item>
可能重复 Changing EditText bottom line color with appcompat v7
注意:- 如果您使用 AppCompat v22
支持库,您可以在 EditText 中指定主题,例如:android:theme="@style/Theme.App.Base
。这将确保样式不会同时影响布局中您不想更改的其他视图。
您需要为此
使用StateListDrawable
- StateListDrawable 是 XML 中定义的可绘制对象,它使用多个不同的图像来表示相同的图形,具体取决于对象的状态。
示例代码
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint Text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edt_bg"
android:visibility="visible" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint Text">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edt_bg"
android:visibility="visible" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
@drawable/edt_bg
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/edt_bg_selected" android:state_focused="true" />
<item android:drawable="@drawable/edt_bg_normal" android:state_focused="false" />
</selector>
@drawable/edt_bg_selected
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#0fe4e4" />
<solid android:color="#00ffffff" />
</shape>
</item>
</layer-list>
@drawable/edt_bg_normal
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#000" />
<solid android:color="#00ffffff" />
</shape>
</item>
</layer-list>
输出
注意
根据您的要求使用android:width="2dp"
和android:color="#000"
设置Thickness宽度和线条颜色你的 TextInputEditText
这个小功能或许能帮到你
fun hintRemover(textInputLayout: TextInputLayout, textInputEditText: TextInputEditText, text: String) {
textInputEditText.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
textInputLayout.hint = null
}
else {
textInputLayout.hint = text
}
}
}