如何更改TextInputEditText的底线颜色和提示颜色?

时间:2016-12-19 06:39:55

标签: java android colors android-textinputlayout android-textinputedittext

我使用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>

java代码:

      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);
            }
        });
    }

}

输出: enter image description here

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:5)

colors.xml文件中定义颜色

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

并在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>

答案 1 :(得分:0)

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"
>