Disable/Remove TextInputLayout 中的浮动标签提示文本 XML
Disable/Remove floating label hint text in TextInputLayout XML
这似乎违反直觉,但有没有办法禁用或删除 TextInputLayout
中的浮动标签提示?我想使用 TextInputLayout
而不是 EditText
的原因是 TextInputLayout
提供的计数器。
这是我目前的情况:
<android.support.design.widget.TextInputLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:counterEnabled="true"
app:counterMaxLength="100">
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:inputType="textMultiLine|textCapSentences"
android:maxLength="100"
android:scrollbars="vertical"
android:hint="This is my cool hint"/>
</android.support.design.widget.TextInputLayout>
我认为这对你有帮助:
textContainer.setHintAnimationEnabled(false);
将设计库更新到 v23 并在 TextInputLayout
中添加 app:hintAnimationEnabled="false"
可以通过三种方式实现这一目标:
1 将 TextInputLayout
上的 android:hint
设置为 space _
字符,并保持 android:hint="This is my cool hint"
设置在 EditText
.
<android.support.design.widget.TextInputLayout
....
....
android:hint=" "> <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</android.support.design.widget.TextInputLayout>
之所以有效,是因为 TextInputLayout
在使用 EditText's
提示之前执行了以下检查:
// If we do not have a valid hint, try and retrieve it from the EditText
if (TextUtils.isEmpty(mHint)) {
setHint(mEditText.getHint());
// Clear the EditText's hint as we will display it ourselves
mEditText.setHint(null);
}
通过设置 android:hint=" "
,if (TextUtils.isEmpty(mHint))
的计算结果为 false
,并且 EditText
保留其提示。
2 第二种选择是继承 TextInputLayout
并覆盖它的 addView(View child, int index, ViewGroup.LayoutParams params)
方法:
public class CTextInputLayout extends TextInputLayout {
public CTextInputLayout(Context context) {
this(context, null);
}
public CTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
// cache the actual hint
CharSequence hint = ((EditText)child).getHint();
// remove the hint for now - we don't want TextInputLayout to see it
((EditText)child).setHint(null);
// let `TextInputLayout` do its thing
super.addView(child, index, params);
// finally, set the hint back
((EditText)child).setHint(hint);
} else {
// Carry on adding the View...
super.addView(child, index, params);
}
}
}
然后使用您的自定义 CTextInoutLayout
而不是设计支持库中的自定义:
<your.package.name.CTextInputLayout
....
.... > <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</your.package.name.CTextInputLayout>
3 第三,可能也是最直接的方法是进行以下调用:
// remove hint from `TextInputLayout`
((TextInputLayout)findViewById(R.id.textContainer)).setHint(null);
// set the hint back on the `EditText`
// The passed `String` could also be a string resource
((EditText)findViewById(R.id.myEditText)).setHint("This is my cool hinttt.");
您可以调用支持库的起始版本 23.2.0
setHintEnabled(false)
或将它放在您的 TextInputLayout xml 中:
app:hintEnabled="false"
虽然这个名字可能会让你认为它删除了所有提示,但它只是删除了浮动提示。
使用 com.google.android.material 你可以通过
隐藏
<com.google.android.material.textfield.TextInputLayout
....
app:hintAnimationEnabled="false"
app:hintEnabled="false"
>
<com.google.android.material.textfield.TextInputEditText
........
android:hint="@string/label_hint"
/>
</com.google.android.material.textfield.TextInputLayout>
我已经尝试了所有的答案,但现在没有一个能奏效(特别是 com.google.android.material:material:1.1.0-beta01)。即使我们更改了 TextInputLayout 中的 EditText 添加逻辑,我们在字段顶部有空的 space,空白了一半的文本和提示。现在我有了解决问题的方法。主要是EditText中的填充,如mentioned in material.io:
<com.google.android.material.textfield.TextInputLayout
. . .
android:layout_height="wrap_content"
app:hintEnabled="false"
app:startIconDrawable="@drawable/ic_search_black"
app:endIconMode="clear_text">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:hint="@string/showcase_search_hint_text"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
它允许我们使用自定义视图
实现带有搜索图标和文本删除按钮的 'SearchView' 样视图,没有任何丑陋的东西 "magic"
myEditText.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) textContainer.hint = null
else myEditText.hint = getString(R.string.your_string)
}
它使您的提示在您的 textInputLayout 中完美无缺,因为如果您想让它与 app:hintEnabled="false" 一起消失,那会使您的 textInputLayout 不酷:)
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etemailLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" ">
<EditText
android:id="@+id/txtemail"
style="@style/login_edittext"
android:hint="Enter Your Email Address"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etPasswordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" "
android:scrollbars="vertical"
app:counterEnabled="true"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/txtpassword"
style="@style/login_edittext"
android:fontFamily="@font/ubuntu"
android:hint="Enter Password"
android:inputType="textPassword"
android:maxLength="8"
android:scrollbars="vertical" />
</com.google.android.material.textfield.TextInputLayout>
为了将样式应用到您的 EditText 中,请将下面的代码放在 Styles.xml ..
<style name="login_edittext">
<item name="android:layout_marginTop">15dp</item>
<item name="android:background">@drawable/edittext_background</item>
<item name="android:textColorHint">#FFFFFF</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
为了背景效果在 drawable 中创建 edittext_background.xml..
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="7dp" />
<solid android:color="#80ffffff" />
<padding android:left="10dp" android:right="10dp"
android:top="10dp" android:bottom="10dp" />
<stroke android:width="2dp" android:color="#FFFFFF" />
</shape>
如果有人遇到像我这样的问题:
标签使文本填充显示错误然后执行此操作:
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:paddingTop="@dimen/default_padding"
android:paddingBottom="@dimen/default_padding"
android:layout_height="wrap_content"
android:hint="@string/search_hint" />
将 padding top 和 padding bottom 添加到 TextInputEditText
将解决问题
@Gauthier 的回答是正确的,但如果您不想要浮动提示,而是想要文本编辑前的提示,那么除了禁用 TextInputLayout 的提示之外,您还应该在 EditText 小部件中提供提示。
我想这回答了上面评论中一些人提出的一些问题。
如果您使用了 app:hintEnabled="false" 然后在 EditText 中设置 android:paddingTop="8dp" 并且 boom 顶部提示 space 消失了,这对我有用希望这个也适合你
这似乎违反直觉,但有没有办法禁用或删除 TextInputLayout
中的浮动标签提示?我想使用 TextInputLayout
而不是 EditText
的原因是 TextInputLayout
提供的计数器。
这是我目前的情况:
<android.support.design.widget.TextInputLayout
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:counterEnabled="true"
app:counterMaxLength="100">
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:inputType="textMultiLine|textCapSentences"
android:maxLength="100"
android:scrollbars="vertical"
android:hint="This is my cool hint"/>
</android.support.design.widget.TextInputLayout>
我认为这对你有帮助:
textContainer.setHintAnimationEnabled(false);
将设计库更新到 v23 并在 TextInputLayout
app:hintAnimationEnabled="false"
可以通过三种方式实现这一目标:
1 将 TextInputLayout
上的 android:hint
设置为 space _
字符,并保持 android:hint="This is my cool hint"
设置在 EditText
.
<android.support.design.widget.TextInputLayout
....
....
android:hint=" "> <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</android.support.design.widget.TextInputLayout>
之所以有效,是因为 TextInputLayout
在使用 EditText's
提示之前执行了以下检查:
// If we do not have a valid hint, try and retrieve it from the EditText
if (TextUtils.isEmpty(mHint)) {
setHint(mEditText.getHint());
// Clear the EditText's hint as we will display it ourselves
mEditText.setHint(null);
}
通过设置 android:hint=" "
,if (TextUtils.isEmpty(mHint))
的计算结果为 false
,并且 EditText
保留其提示。
2 第二种选择是继承 TextInputLayout
并覆盖它的 addView(View child, int index, ViewGroup.LayoutParams params)
方法:
public class CTextInputLayout extends TextInputLayout {
public CTextInputLayout(Context context) {
this(context, null);
}
public CTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
// cache the actual hint
CharSequence hint = ((EditText)child).getHint();
// remove the hint for now - we don't want TextInputLayout to see it
((EditText)child).setHint(null);
// let `TextInputLayout` do its thing
super.addView(child, index, params);
// finally, set the hint back
((EditText)child).setHint(hint);
} else {
// Carry on adding the View...
super.addView(child, index, params);
}
}
}
然后使用您的自定义 CTextInoutLayout
而不是设计支持库中的自定义:
<your.package.name.CTextInputLayout
....
.... > <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</your.package.name.CTextInputLayout>
3 第三,可能也是最直接的方法是进行以下调用:
// remove hint from `TextInputLayout`
((TextInputLayout)findViewById(R.id.textContainer)).setHint(null);
// set the hint back on the `EditText`
// The passed `String` could also be a string resource
((EditText)findViewById(R.id.myEditText)).setHint("This is my cool hinttt.");
您可以调用支持库的起始版本 23.2.0
setHintEnabled(false)
或将它放在您的 TextInputLayout xml 中:
app:hintEnabled="false"
虽然这个名字可能会让你认为它删除了所有提示,但它只是删除了浮动提示。
使用 com.google.android.material 你可以通过
隐藏<com.google.android.material.textfield.TextInputLayout
....
app:hintAnimationEnabled="false"
app:hintEnabled="false"
>
<com.google.android.material.textfield.TextInputEditText
........
android:hint="@string/label_hint"
/>
</com.google.android.material.textfield.TextInputLayout>
我已经尝试了所有的答案,但现在没有一个能奏效(特别是 com.google.android.material:material:1.1.0-beta01)。即使我们更改了 TextInputLayout 中的 EditText 添加逻辑,我们在字段顶部有空的 space,空白了一半的文本和提示。现在我有了解决问题的方法。主要是EditText中的填充,如mentioned in material.io:
<com.google.android.material.textfield.TextInputLayout
. . .
android:layout_height="wrap_content"
app:hintEnabled="false"
app:startIconDrawable="@drawable/ic_search_black"
app:endIconMode="clear_text">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:hint="@string/showcase_search_hint_text"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
它允许我们使用自定义视图
实现带有搜索图标和文本删除按钮的 'SearchView' 样视图,没有任何丑陋的东西 "magic"myEditText.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) textContainer.hint = null
else myEditText.hint = getString(R.string.your_string)
}
它使您的提示在您的 textInputLayout 中完美无缺,因为如果您想让它与 app:hintEnabled="false" 一起消失,那会使您的 textInputLayout 不酷:)
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etemailLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" ">
<EditText
android:id="@+id/txtemail"
style="@style/login_edittext"
android:hint="Enter Your Email Address"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etPasswordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=" "
android:scrollbars="vertical"
app:counterEnabled="true"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/txtpassword"
style="@style/login_edittext"
android:fontFamily="@font/ubuntu"
android:hint="Enter Password"
android:inputType="textPassword"
android:maxLength="8"
android:scrollbars="vertical" />
</com.google.android.material.textfield.TextInputLayout>
为了将样式应用到您的 EditText 中,请将下面的代码放在 Styles.xml ..
<style name="login_edittext">
<item name="android:layout_marginTop">15dp</item>
<item name="android:background">@drawable/edittext_background</item>
<item name="android:textColorHint">#FFFFFF</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
为了背景效果在 drawable 中创建 edittext_background.xml..
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="7dp" />
<solid android:color="#80ffffff" />
<padding android:left="10dp" android:right="10dp"
android:top="10dp" android:bottom="10dp" />
<stroke android:width="2dp" android:color="#FFFFFF" />
</shape>
如果有人遇到像我这样的问题:
标签使文本填充显示错误然后执行此操作:
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:paddingTop="@dimen/default_padding"
android:paddingBottom="@dimen/default_padding"
android:layout_height="wrap_content"
android:hint="@string/search_hint" />
将 padding top 和 padding bottom 添加到 TextInputEditText
将解决问题
@Gauthier 的回答是正确的,但如果您不想要浮动提示,而是想要文本编辑前的提示,那么除了禁用 TextInputLayout 的提示之外,您还应该在 EditText 小部件中提供提示。
我想这回答了上面评论中一些人提出的一些问题。
如果您使用了 app:hintEnabled="false" 然后在 EditText 中设置 android:paddingTop="8dp" 并且 boom 顶部提示 space 消失了,这对我有用希望这个也适合你