我面临一个奇怪的问题,其中有一个InputTextLayout和一个EditText,我正在尝试实现如下所示(如下图所示)(来自材料设计准则的图像:https://material.io/guidelines/components/text-fields.html#text-fields-layout),其中有两个单独的提示。我这样做是通过将android:hint添加到两个布局中。这可以正常工作,但是当焦点移开该位置时,“标签”将向下移动并与“占位符”文本重叠。 (仅当用户未提供任何输入且编辑文本为空时,两个提示才重叠)。有关如何解决此问题的任何指示?
作为要求,我需要两个提示都存在,并且“标签”不应向下移动焦点更改。两个提示都应保持在原位置
<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>
据我所知,我们不能如你所愿。
因为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.stackoverflow.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Lable"
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>
科特林码
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");
}
}
});
<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
中删除提示>
仅在android:hint=""
或AppcompatEditText
中使用TextInputLayout
<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
尝试分别为AppCompatEditText和TextInputLayout设置提示: