EditText getHint() returns null when using design support library(使用设计支持库时 EditText getHint() 返回 null)
问题描述
将 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用设计支持库时 EditText getHint() 返回 null


- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01