Mic*_*cro 45 android android-textinputlayout
这可能看起来违反直觉,但有没有办法禁用或删除浮动标签提示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>
Run Code Online (Sandbox Code Playgroud)
Gau*_*ier 117
从支持库的23.2.0版开始,您可以调用
setHintEnabled(false)
Run Code Online (Sandbox Code Playgroud)
或者将它放在TextInputLayout xml中:
app:hintEnabled="false"
Run Code Online (Sandbox Code Playgroud)
虽然名称可能会让您认为它删除了所有提示,但它只是移除了浮动的提示.
相关文档和问题:http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setHintEnabled(boolean)
https://code.google.com/p/android/issues/detail?id=181590
Vik*_*ram 33
实现这一目标可能有三种方法:
1设置android:hint
上TextInputLayout
,以一个空格_
字符,并始终保持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>
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为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);
}
Run Code Online (Sandbox Code Playgroud)
通过设置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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用您的自定义CTextInoutLayout
而不是设计支持库中的自定义:
<your.package.name.CTextInputLayout
....
.... > <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</your.package.name.CTextInputLayout>
Run Code Online (Sandbox Code Playgroud)
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.");
Run Code Online (Sandbox Code Playgroud)
小智 8
如果您使用了 app:hintEnabled="false" 然后还在 EditText 中设置 android:paddingTop="8dp" 并且繁荣顶部提示空间消失了,这对我有用希望这对你也有用
使用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>
Run Code Online (Sandbox Code Playgroud)
我已经尝试了所有答案,但现在都没有工作(特别是 com.google.android.material:material:1.1.0-beta01)。即使我们对 TextInputLayout 中的 EditText 添加逻辑进行了更改,我们在字段顶部仍有空白区域,空白的文本和提示的一半。现在我有了解决问题的办法。最主要的是在EditText上的填充,如提及在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>
Run Code Online (Sandbox Code Playgroud)
它允许我们使用搜索图标和文本删除按钮实现类似“SearchView”的视图,而没有任何带有自定义视图的丑陋“魔法”
归档时间: |
|
查看次数: |
35307 次 |
最近记录: |