有加app:hintAnimationEnabled="false"
可能有三種方法可以實現這一點:
在TextInputLayout
上設置android:hint
至空格_
字符,並將android:hint="This is my cool hint"
設置爲EditText
。
<android.support.design.widget.TextInputLayout
....
....
android:hint=" "> <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</android.support.design.widget.TextInputLayout>
這工作,因爲TextInputLayout
使用EditText's
提示之前進行以下檢查:
// If we do not have a valid hint, try and retrieve it from the EditText
if (TextUtils.isEmpty(mHint)) {
setHint(mEditText.getHint());
// Clear the EditText's hint as we will display it ourselves
mEditText.setHint(null);
}
通過設置android:hint=" "
,if (TextUtils.isEmpty(mHint))
計算結果爲false
,並且EditText
保留其暗示。
第二個選項是子類TextInputLayout
並覆蓋其addView(View child, int index, ViewGroup.LayoutParams params)
方法:
<your.package.name.CTextInputLayout
....
.... > <<----------
<EditText
....
....
android:hint="This is my cool hint"/> <<----------
</your.package.name.CTextInputLayout>
:
public class CTextInputLayout extends TextInputLayout {
public CTextInputLayout(Context context) {
this(context, null);
}
public CTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
// cache the actual hint
CharSequence hint = ((EditText)child).getHint();
// remove the hint for now - we don't want TextInputLayout to see it
((EditText)child).setHint(null);
// let `TextInputLayout` do its thing
super.addView(child, index, params);
// finally, set the hint back
((EditText)child).setHint(hint);
} else {
// Carry on adding the View...
super.addView(child, index, params);
}
}
}
然後從設計支持庫使用自定義CTextInoutLayout
,而不是一3第三,也許最直接的方法是進行以下調用:
// remove hint from `TextInputLayout`
((TextInputLayout)findViewById(R.id.textContainer)).setHint(null);
// set the hint back on the `EditText`
// The passed `String` could also be a string resource
((EditText)findViewById(R.id.myEditText)).setHint("This is my cool hinttt.");
最好的主意,以編程方式,你必須給予提示和textChange事件時間向空提示的編輯框這個想法可以幫助你 –
問題是暗示色彩。輸入內容時會變成白色。請更改提示顏色或更改背景顏色。打字時你會看到提示。 –