![](/img/trans.png)
[英]Android - How to get hint size of TextInputLayout programmatically when focused?
[英]TextInputLayout doesn't show hint when focused if the hint was set programmatically
我正在這樣做:
<android.support.design.widget.TextInputLayout
android:id="@+id/inputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
然后這個:
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
mEmailView.setHint("Email");
提示顯示在EditText
上,但聚焦時不在其上方。 我可以在TextInputLayout
上設置提示,但隨后將其與EditText
內的提示同時獲取。 這是一個錯誤還是我做錯了什么?
謝謝。
而不是在子EditText上使用setHint()
,應該直接在TextInputLayout
上調用它:
TextInputLayout textInputLayout = (TextInputLayout)findViewById(R.id.inputLayout);
textInputLayout.setHint("Email");
這應該是開箱即用的,不需要其他“黑客”。
TextInputLayout
確實嘗試從內部EditText
提示中初始化其提示,但是它僅在創建時執行,並且如果找不到,則不執行任何操作,因此這就是為什么您需要通過調用setHintEnabled()
使它“重試”的setHintEnabled()
...
我想文檔應該更清楚地描述這種細微差別。
明白了,對我來說似乎是個bug。
基本上我必須:
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
mEmailView.setHint("Email");
TextInputLayout textInputLayout= (TextInputLayout)
findViewById(R.id.inputLayout);
textInputLayout.setHintEnabled(false);
textInputLayout.setHintEnabled(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.