0

我有一个嵌入片段的activity_edit_profile.xml布局,但 EditText 提示会永久显示。当 EditText 字段处于焦点或插入文本时,如何摆脱提示?

    <?xml version="1.0" encoding="utf-8"?>
        <android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/DrawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:context="com.getbase.floatingactionbutton.sample.EditProfileActivity"
            tools:ignore="MergeRootFrame">

            <include layout="@layout/fragment_edit_profile"/>

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

fragment_edit_profile.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="16dp"
              android:orientation="vertical"
              tools:context="com.getbase.floatingactionbutton.sample.EditProfileActivity$PlaceholderFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:theme="@style/TextLabel">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="First Name"
                android:inputType="textCapWords"
                android:nextFocusDown="@+id/register_input_lastname"
                />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:theme="@style/TextLabel">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/register_input_lastname"
                android:hint="Last Name"
                android:inputType="textCapWords"/>
        </android.support.design.widget.TextInputLayout>

    </LinearLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:theme="@style/TextLabel">

        <android.support.v7.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Email"
            android:inputType="textEmailAddress" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:theme="@style/TextLabel">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="textPassword" />
    </android.support.design.widget.TextInputLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="2"
            android:theme="@style/TextLabel">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Country Code"
                android:inputType="number"
                android:nextFocusDown="@+id/mobile_number"/>

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

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:theme="@style/TextLabel">

            <EditText
                android:id="@+id/mobile_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Mobile"
                android:inputType="phone"/>

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

    </LinearLayout>
</LinearLayout>

editText 提示不断显示并与 textInput 重叠。

在此处输入图像描述

[编辑]

这是 TextLabel 样式:

<style name="TextLabel" parent="@android:style/TextAppearance">
    <item name="android:textColorHint">@color/half_black</item>
    <item name="android:textSize">14dp</item>
    <item name="colorAccent">@color/ColorPrimary</item>
    <item name="colorControlActivated">@color/ColorPrimary</item>
</style>
4

3 回答 3

0

您可以将 focusChangeListener 附加到您的EditText字段。

让我们举个例子First Name。为其分配 id firstName。现在,当它成为焦点时,将提示文本设置为空字符串。当它失去焦点时,将提示文本改回First Name

EditText firstName = (EditText)findViewById(R.id.firstName);
firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
            firstName.setHint("");
        }
        else {
            firstName.setHint("First Name");
        }
    }
});
于 2016-01-30T06:22:23.133 回答
0

可能问题在于您为TextInputLayout样式设置的主题应该如下所示 -

    <style name="AppTheme.EditText">
        <item name="colorPrimary">#FF4081</item>
        <item name="colorPrimaryDark">#3F51B5</item>
        <item name="colorAccent">#303F9F</item>
    </style>
于 2016-01-30T11:00:31.810 回答
-1

删除您的 TextInputLayout 小部件。仅添加 EditText。

   <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="First Name"
            android:inputType="textCapWords"
            android:nextFocusDown="@+id/register_input_lastname"
            />
于 2016-02-01T11:54:08.763 回答