4

我正在使用 TextInputLayout 和 TetInputEditText 来获得浮动提示。现在我想更改编辑文本的提示和底线的颜色。我希望它在用户点击编辑文本时得到改变。

所以我试图改变它的颜色 onClickListener 的一个编辑文本。但是我没有发现颜色有任何变化。

xml布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical">


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

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:paddingTop="05dp">
        <android.support.design.widget.TextInputEditText
            android:layout_width="240dp"
            android:layout_height="45dp"
            android:id="@+id/editTextItemName"
            android:layout_gravity="center_horizontal"
            android:hint="@string/item_name"
            android:textSize="12sp" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

爪哇代码:

      public void setUpUI(){

        edt_Item_Name = (TextInputEditText) findViewById(R.id.editTextItemName);
        edt_Item_quantity = (TextInputEditText)findViewById(R.id.editTextItemQuantity);
        edt_Item_Unit = (TextInputEditText)findViewById(R.id.editTextItemUnit);

        textInput_Item_name = (TextInputLayout)findViewById(R.id.input_layout_item_name);
        textInput_Item_quantity = (TextInputLayout)findViewById(R.id.input_layout_item_quantity);
        textInput_Item_Unit = (TextInputLayout)findViewById(R.id.input_layout_item_unit);

        edt_Item_Name.getBackground().mutate().setColorFilter(ContextCompat.getColor(this, R.color.edtColor), PorterDuff.Mode.SRC_ATOP);

        edt_Item_Name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_Item_Name.setHintTextColor(ContextCompat.getColor(SearchActivity.this, R.color.edtColor));
                edt_Item_Name.getBackground().mutate().setColorFilter(ContextCompat.getColor(SearchActivity.this, R.color.edtColor), PorterDuff.Mode.SRC_ATOP);
            }
        });
    }

}

输出 : 在此处输入图像描述

我怎样才能做到这一点?

4

3 回答 3

3

colors.xml在文件中定义颜色

<color name="colorControlNormal">#c5c5c5</color>
<color name="colorControlActivated">@color/accent</color>
<color name="colorControlHighlight">@color/accent</color>

并创造风格styles.xml

<style name="TextAppearence.App.TextInputLayout"  parent="@android:style/TextAppearance">
     <item name="android:textColor">@color/main_color</item>
</style>

并应用于您的TextInputLayout

 app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
 android:textColorHint="#0072BA"

将您的代码更改为:

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

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_item_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:paddingTop="05dp"
            app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"
            android:textColorHint="#0072BA">
        <android.support.design.widget.TextInputEditText
            android:layout_width="240dp"
            android:layout_height="45dp"
            android:id="@+id/editTextItemName"
            android:layout_gravity="center_horizontal"
            android:hint="@string/item_name"
            android:textSize="12sp" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>
于 2016-12-19T06:49:57.040 回答
0

要更改提示颜色,请使用:

android:textColorHint="@color/blue"

在您的 TextInputLayout 中,例如:

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/blue"
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Text"/>
    </com.google.android.material.textfield.TextInputLayout>
      

于 2022-02-12T07:13:23.573 回答
-1

在中创建样式styles.xml

<style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined" parent="">
    <item name="colorControlActivated">@color/your_color</item>
</style>

在xml中添加

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/email_layout"
        android:hint="Enter Your Email*"
        app:hintTextAppearance="@style/MyHintStyle"
        android:textColorHint="#6a6a6a"
        android:theme="@style/ThemeOverlay.AppTheme.TextInputEditText.Outlined"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
>
于 2021-03-16T17:37:17.307 回答