Khi sử dụng EditText kết hợp với Design lib's (ver 22.2.1) TextInputLayout nhận gợi ý lập trình trả về null.EditText getHint() trả về null khi sử dụng thư viện hỗ trợ thiết kế
Tôi đang cố gắng gắn dấu hoa thị '*' vào trường bắt buộc theo chương trình, do đó EditText.getHint()
nhưng thực tế là nó trả về null là một vấn đề trong trường hợp này.
EditText editText = (EditText) findViewById(R.id.edit2);
String hint = String.format("%s *", editText.getHint());
editText.setHint(hint);
Một mã minh họa đơn giản: 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");
phụ thuộc: biên dịch 'com.android.support:design: 22.2.1 '
Vấn đề liên quan trước đây here!
Có [nhiều vấn đề gợi ý liên quan đến với 'TextInputLayout' ] (https://code.google.com/p/android/issues/list?can=1&q=TextInputLayout+hint&colspec=ID+Type+Status+Owner+Summary+Stars&cells=tiles). Bạn có thể xem xét thử [triển khai nhãn nổi khác] (http://android-arsenal.com/search?q=floating+label). – CommonsWare
Vấn đề rõ ràng là setHint() cũng như không phản ánh trên gợi ý được định cấu hình trước đó trên TextInputLayout – dobridog