Android TextInputLayout 提示与 EditText 提示重叠
Android TextInputLayout hint overlaps EditText hint
我遇到了一个奇怪的问题,我有一个 InputTextLayout 和一个 EditText,我正在尝试实现类似的东西(如下图所示)(图片来自 material 设计指南:https://material.io/guidelines/components/text-fields.html#text-fields-layout),其中有两个单独的提示。我通过向两种布局添加 android:hint 来做到这一点。这很好用,但是当焦点从这里移开时,"label" 向下移动并与 "placeholder" 文本重叠。 (仅当用户未提供任何输入且编辑文本为空时 - 两个提示重叠)。关于如何解决这个问题的任何指示?
作为一项要求,我需要两个提示都存在,并且 "Label" 不应在焦点更改时下移。两个提示都应该保留在它们的位置
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
使用下面的代码。它应该可以正常工作。
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textColorHint="@color/white">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="text"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</android.support.design.widget.TextInputLayout>
从 AppcompatEditText
或 TextInputLayout
中删除提示
仅在 AppcompatEditText
或 TextInputLayout
中使用 android:hint=""
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
了解更多信息 check this link
据我所知,我们不能如您所愿。
因为 TextInputLayout 被设计为在获得焦点后浮动提示因此,一旦它上升,占位符中就没有任何内容。我们可以按照您的要求进行如下略微更改。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.Whosebug.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Label"
android:textColor="@color/colorPrimary" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
app:hintEnabled="false">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:hint="Place Holder"
/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
.
完美一个!!!
xml代码
<android.support.design.widget.TextInputLayout
android:id="@+id/inputLayoutPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Phone Number">
<android.support.design.widget.TextInputEditText
android:id="@+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="+91"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
Kotlin 代码
edtPhone.hint=""
edtPhone.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
inputLayoutPhone.isHintEnabled = hasFocus
if(hasFocus){
edtPhone.hint="+91"
}else{
edtPhone.hint= "Phone Number"
}
}
Java代码
edtPhone.setHint("");
edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
edtPhone.setHint("+91");
}else{
edtPhone.setHint("Phone Number");
}
}
});
尝试为 AppCompatEditText 和 TextInputLayout 分别设置提示:
xml代码:
<android.support.design.widget.TextInputLayout
android:id="@+id/inputLayoutPhone"
...>
<android.support.design.widget.TextInputEditText
android:id="@+id/edtPhone"
... />
</android.support.design.widget.TextInputLayout>
Kotlin 代码:
inputLayoutPhone.setHint("Label")
edtPhone.setHint("Placeholder")
现在 material 组件 (alpha03) 支持占位符文本:
dependencies {
// ...
implementation 'com.google.android.material:material:1.2.0-alpha03'
// ...
}
<com.google.android.material.textfield.TextInputLayout
...
app:placeholderText="Placeholder text">
对我来说,我找到的最佳解决方案已在
中得到解答
我遇到了一个奇怪的问题,我有一个 InputTextLayout 和一个 EditText,我正在尝试实现类似的东西(如下图所示)(图片来自 material 设计指南:https://material.io/guidelines/components/text-fields.html#text-fields-layout),其中有两个单独的提示。我通过向两种布局添加 android:hint 来做到这一点。这很好用,但是当焦点从这里移开时,"label" 向下移动并与 "placeholder" 文本重叠。 (仅当用户未提供任何输入且编辑文本为空时 - 两个提示重叠)。关于如何解决这个问题的任何指示?
作为一项要求,我需要两个提示都存在,并且 "Label" 不应在焦点更改时下移。两个提示都应该保留在它们的位置
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
使用下面的代码。它应该可以正常工作。
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:textColorHint="@color/white">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="text"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</android.support.design.widget.TextInputLayout>
从 AppcompatEditText
或 TextInputLayout
仅在 AppcompatEditText
或 TextInputLayout
android:hint=""
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Placeholder"
android:inputType="textCapWords"
/>
</android.support.design.widget.TextInputLayout>
了解更多信息 check this link
据我所知,我们不能如您所愿。
因为 TextInputLayout 被设计为在获得焦点后浮动提示因此,一旦它上升,占位符中就没有任何内容。我们可以按照您的要求进行如下略微更改。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.Whosebug.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Label"
android:textColor="@color/colorPrimary" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
app:hintEnabled="false">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:hint="Place Holder"
/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
完美一个!!!
xml代码
<android.support.design.widget.TextInputLayout
android:id="@+id/inputLayoutPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Phone Number">
<android.support.design.widget.TextInputEditText
android:id="@+id/edtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="+91"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
Kotlin 代码
edtPhone.hint=""
edtPhone.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
inputLayoutPhone.isHintEnabled = hasFocus
if(hasFocus){
edtPhone.hint="+91"
}else{
edtPhone.hint= "Phone Number"
}
}
Java代码
edtPhone.setHint("");
edtPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
edtPhone.setHint("+91");
}else{
edtPhone.setHint("Phone Number");
}
}
});
尝试为 AppCompatEditText 和 TextInputLayout 分别设置提示:
xml代码:
<android.support.design.widget.TextInputLayout
android:id="@+id/inputLayoutPhone"
...>
<android.support.design.widget.TextInputEditText
android:id="@+id/edtPhone"
... />
</android.support.design.widget.TextInputLayout>
Kotlin 代码:
inputLayoutPhone.setHint("Label")
edtPhone.setHint("Placeholder")
dependencies {
// ...
implementation 'com.google.android.material:material:1.2.0-alpha03'
// ...
}
<com.google.android.material.textfield.TextInputLayout
...
app:placeholderText="Placeholder text">
对我来说,我找到的最佳解决方案已在
中得到解答