可以更改浮动EditText提示的文本吗?

时间:2015-08-03 13:57:40

标签: android android-layout android-edittext android-design-library

是否可以根据文本是浮动还是在EditText内部来更改浮动提示的文本?

例如,当字段为空时,我希望看到文本“您的名字”,当提示浮动时,我希望看到文本“名称”。

现在的样子:

<android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your name"
                    android:textColorHint="@color/dark_grey"
                    android:textColor="@color/dark_grey"
                    android:background="@drawable/edittext_bg"
                    >
                    <requestFocus/>
                </EditText>

</android.support.design.widget.TextInputLayout>

2 个答案:

答案 0 :(得分:3)

它将在TextInputLayout和EditText的onFocusChangeListener的帮助下完成,

textInputLayoutEmail.setHint("Enter your Email id");

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                textInputLayoutEmail.setHint("Email");
            }
        });

从xml文件中的EditText中删除提示,

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/dark_grey"
        android:textColor="@color/dark_grey"
        android:background="@drawable/edittext_bg">

        <requestFocus />
    </EditText>

</android.support.design.widget.TextInputLayout>

答案 1 :(得分:0)

EditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub              
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub      
        }
        @Override
        public void afterTextChanged(Editable s) {
            EditText.setHint("Name");    
        }
    });