2015-07-30 27 views
8

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); 

Asterisk overlays the 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!

+1

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

+0

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

Trả lời

10

Trên thực tế di chuyển gợi ý đến cái nhìn mẹ TextInputLayout bao quanh EditText xem:

Bạn có thể nhận được gợi ý như thế này:

android.support.design.widget.TextInputLayout parent = (android.support.design.widget.TextInputLayout) yourEditText.getParent(); 
String hint = parent.getHint().toString(); 

Và nếu bạn muốn thêm * làm cho nó như thế này:

parent.setHint(parent.getHint() + "*"); 

Chúc mừng! :)

+8

Lưu ý rằng nếu bạn đang sử dụng 'TextInputEditText' như được khuyến nghị, bạn phải sử dụng' getParent(). GetParent() 'để thực sự nhận được' TextInputLayout' –

-1

Điều này được sửa trong hỗ trợ thiết kế 23.0.0 nhưng dự án phải được biên dịch cho api 23.

build.gradle

android { 
    compileSdkVersion 23 
    buildToolsVersion '23' 
    .... 
} 
dependencies{ 
    compile 'com.android.support:appcompat-v7:23.0.0' 
... 
} 

Thiết gợi ý trên EditText như trước đây:

<android.support.design.widget.TextInputLayout 
    android:id="@+id/inputField" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/hello_world" 
     android:inputType="text"/> 

Bây giờ thêm một nhân vật phụ để gợi ý:

TextInputLayout inputField = (TextInputLayout) findViewById(R.id.inputLayout); 
String hint = String.format("%s *", inputField.getHint()); 
inputField.setHint(hint); 
+2

Điều này không được sửa trong 23.0.0 – superuserdo

+0

Vấn đề với việc khai báo gợi ý trong TextInputLayout là giá trị gợi ý không hiển thị trừ khi được đặt theo chương trình (như trong ví dụ).Nhưng cuối cùng di chuyển gợi ý để chỉnh sửa văn bản đã làm việc. Sẽ cập nhật câu trả lời, cảm thấy tự do để xác minh. – dobridog

+0

Vấn đề của câu hỏi là về gợi ý EditText của TextInputLayout trả về null. Tôi đã xác minh rằng vấn đề này vẫn tồn tại trong v 23 của thư viện. Ngay cả khi thiết lập gợi ý trên TextInputLayout, gợi ý vẫn trả về null. – superuserdo

0

Gợi ý được liên kết với bố trí của cha mẹ nó

((TextInputLayout) view.getParent()). GetHint()

Các vấn đề liên quan