将 EditText 与 Design lib(版本 22.2.1)结合使用时,TextInputLayout 以编程方式获取提示返回 null.
When using EditText in combination with Design lib's (ver 22.2.1) TextInputLayout getting hint programmatically returns null.
我正在尝试以编程方式将星号*"附加到必填字段,因此 EditText.getHint() 但它返回 null 的事实在这种情况下是一个问题.
I'm trying to append asterisk '*' to a mandatory field programmatically, hence EditText.getHint() but the fact that it returns null is an issue in this case.
EditText editText = (EditText) findViewById(R.id.edit2);
String hint = String.format("%s *", editText.getHint());
editText.setHint(hint);
一个简单的代码说明:布局.xml:
A simple code illustration: Layout.xml:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hello_world"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
Java:
EditText editText = (EditText) findViewById(R.id.edit2);
if (editText.getHint() == null) throw new AssertionError("Hint should not be null");
依赖:编译'com.android.support:design:22.2.1'
dependency: compile 'com.android.support:design:22.2.1'
以前相关的问题 这里!
实际上提示移动到 EditText 视图周围的父视图 TextInputLayout:
Actually the hint moves to the parent view TextInputLayout that surrounds the EditText view:
你可以得到这样的提示:
You can get the hint like this:
android.support.design.widget.TextInputLayout parent = (android.support.design.widget.TextInputLayout) yourEditText.getParent();
String hint = parent.getHint().toString();
如果你想添加 * 让它像这样:
And if you want to add * make it like this:
parent.setHint(parent.getHint() + "*");
编码愉快!:)
这篇关于使用设计支持库时 EditText getHint() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在android中剪切、复制、粘贴Cut, copy, paste in android(在android中剪切、复制、粘贴)
android EditText 融入背景android EditText blends into background(android EditText 融入背景)
更改 EditText 的线条颜色 - AndroidChange Line Color of EditText - Android(更改 EditText 的线条颜色 - Android)
EditText 始终显示带 2 位小数的数字EditText showing numbers with 2 decimals at all times(EditText 始终显示带 2 位小数的数字)
更改光标在展开的 EditText 中的开始位置Changing where cursor starts in an expanded EditText(更改光标在展开的 EditText 中的开始位置)
android中的EditText,adjustPan,ScrollView问题EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView问题)