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
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
