Android EditText左图标在Focus上的颜色变化?

时间:2017-05-27 06:10:50

标签: android

如Google Material Design中所示: Google Material Design Example 我已经实现了相同的功能,但我手动完成了。

这是我的xml:

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

            <TextView
                android:id="@+id/emailIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_10"
                android:textColor="@color/darkGrey"
                android:text="@string/email_icon"
                android:textSize="@dimen/sp_25" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.teafloor.utils.EditTextCustom
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:textSize="@dimen/sp_16" />
            </android.support.design.widget.TextInputLayout>


        </LinearLayout>

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_10"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/passWordIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_10"
                android:textSize="@dimen/sp_25"
                android:textColor="@color/darkGrey"
                android:text="@string/password_icon" />


            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:passwordToggleTint="@color/darkGrey"
                app:passwordToggleDrawable="@drawable/visibility_selector"
                app:passwordToggleEnabled="true">

                <com.teafloor.utils.EditTextCustom
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:textSize="@dimen/sp_16" />

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


        </LinearLayout>

这是我的手工逻辑:

  emailEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            emailIcon.setTextColor(getResources().getColor(R.color.colorAccent));
            if (!passwordEditText.getText().toString().trim().equals("")) {
                passwordIcon.setTextColor(getResources().getColor(R.color.colorAccent));
            }else {
                passwordIcon.setTextColor(getResources().getColor(R.color.darkGrey));
            }
        }
    });

passwordEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            passwordIcon.setTextColor(getResources().getColor(R.color.colorAccent));
            if (!emailEditText.getText().toString().trim().equals("")) {
                emailIcon.setTextColor(getResources().getColor(R.color.colorAccent));
            }else {
                emailIcon.setTextColor(getResources().getColor(R.color.darkGrey));
            }
        }
    });

这里我在onFocusChangeListener中手动更改每个edittext的颜色,问题是如果我有更多的edittexts,我必须检查每个edittext的空状态,然后改变它的颜色。

我必须将图标保留在edittext之外,因此我无法使用drawableLeft或drawableStart。

我实施的内容对我来说很好,但我正在寻找一种更简单的方法,就像google's password toggle feature

一样

2 个答案:

答案 0 :(得分:1)

使用

emailEditText.setOnFocusChangeListener(this);
passwordEditText.setOnFocusChangeListener(this);

并使用覆盖方法实现OnFocusChangeListener

@Override
public void onFocusChange(View view, boolean b) {
    ViewGroup parent = (ViewGroup) view.getParent();
    View child = parent.getChildAt(0);
    if (b) {
        if (child instanceof TextView) {
            ((TextView) child).setTextColor(getResources().getColor(R.color.colorAccent));
        }
    }else{
        EditText editText = (EditText) view;
        if(editText.getText().toString().isEmpty()){
            if (child instanceof TextView) {
                ((TextView) child).setTextColor(getResources().getColor(R.color.darkGrey));
            }
        }else{
            if (child instanceof TextView) {
                ((TextView) child).setTextColor(getResources().getColor(R.color.colorAccent));
            }
        }
    }
}

答案 1 :(得分:0)

您可以为EditText添加主题

&#13;
&#13;
<style name="EditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">@color/primary</item>
    <item name="colorControlActivated">@color/primary</item>
    <item name="colorControlHighlight">@color/primary</item>
</style>

and use in your Edittext
<EditText
   .......................
    android:theme="@style/EditTextTheme">

</EditText>
&#13;
&#13;
&#13;