15

我已经尝试了很多,但无法使用from 支持使工作成为AutoCompleteTextView浮动提示。TextInputLayout

有可能还是我需要使用外部库?

4

2 回答 2

29

不 。

设计库本身就够了

包括这样

<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>
于 2015-08-13T13:00:44.303 回答
0

可能有点晚了,但这是诀窍:

创建以下类,

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>
于 2016-03-24T21:08:48.950 回答