TextInputLayout和AutocompleteTextView的单独提示

时间:2016-11-15 12:41:08

标签: android android-textinputlayout

我想告诉用户他们应该键入位置的模式,如下所示

enter image description here

所以我在TextInputLayout中使用了AutocompleteTextView,如下面的代码

<android.support.design.widget.TextInputLayout
    android:id="@+id/profile_youraddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/profile_emailaddress"
    android:layout_marginTop="@dimen/margin_10"
        android:hint="@string/your_address"
    android:textColor="@color/home_primary_color">

    <AutoCompleteTextView
        android:id="@+id/profile_addresstext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Malviya Nagar, New Delhi, Delhi"
        android:singleLine="true" />
</android.support.design.widget.TextInputLayout>

问题是,当TextInputLayout不在焦点时,它的两个提示相互重叠,如下所示 enter image description here

如何在AutocompleteTextView的非焦点状态提示中覆盖TextInputLayout&#39提示?任何帮助将不胜感激。

我在寻找:

处于焦点时,提示应为&#34;您的地址&#34; (&#34; Malviya Nagar,新德里,德里&#34;应该隐藏)并且当焦点提示应该是&#34; Malviya Nagar,新德里,德里&#34;。

1 个答案:

答案 0 :(得分:2)

使用OnFocusChangeListener:

TextInputLayout text;
AutoCompleteTextView auto;
View.OnFocusChangeListener listener;

     text = (TextInputLayout) findViewById(R.id.profile_youraddress);
            auto = (AutoCompleteTextView) findViewById(R.id.profile_addresstext);
            auto.setFocusable(true);

            listener = new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus) {
                        auto.setHint("Malviya Nagar, New Delhi, Delhi");
                        text.setHint("");
                    }else {
                        auto.setHint("");
                        text.setHint("Your address");
                    }
                }
            };

            auto.setOnFocusChangeListener(listener);
相关问题