我已经尝试了很多,但无法使用from 支持使工作成为AutoCompleteTextView
浮动提示。TextInputLayout
有可能还是我需要使用外部库?
我已经尝试了很多,但无法使用from 支持使工作成为AutoCompleteTextView
浮动提示。TextInputLayout
有可能还是我需要使用外部库?
不 。
包括这样
<android.support.design.widget.TextInputLayout
android:id="@+id/til_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="125dp"
>
<AutoCompleteTextView
android:id="@+id/auto_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Id"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
可能有点晚了,但这是诀窍:
创建以下类,
public class AutoCompleteTextInputLayout extends TextInputLayout {
private boolean mIsHintSet;
private CharSequence mHint;
public AutoCompleteTextInputLayout(Context context) {
super(context);
}
public AutoCompleteTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
mHint = ((EditText) child).getHint();
}
super.addView(child, index, params);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!mIsHintSet && ViewCompat.isLaidOut(this)) {
setHint(null);
EditText editText = getEditText();
if (editText == null) {
return;
}
CharSequence currentEditTextHint = editText.getHint();
if (!TextUtils.isEmpty(currentEditTextHint)) {
mHint = currentEditTextHint;
editText.setHint("");
}
setHint(mHint);
mIsHintSet = true;
}
}
}
现在将此行添加到您的布局文件中,看看魔术
<com.example.AutoCompleteTextInputLayout
android:id="@+id/text_input_autocomplete"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/autocomplete_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColorHint="@color/textColorDarkHint"/>
</com.example.AutoCompleteTextInputLayout>