浮动edittext设置提示的alpha值

时间:2016-07-30 15:09:14

标签: android android-edittext alpha floating

我试图通过将其alpha值设置为0.3来设置透明度以隐藏浮动edittext的颜色。但是它不起作用。我没有看到editext的提示颜色有任何alpha变化。

以下是代码

<android.support.design.widget.TextInputLayout
        android:id="@+id/input_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mobile_email"
        android:layout_marginLeft="@dimen/dp15"
        android:layout_marginRight="@dimen/dp15">

        <EditText
            android:id="@+id/number"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:textColor="@color/numxtcolor"
            android:alpha="0.3"
            android:hint="@string/mobilehint"
            android:textSize="@dimen/sp16"
            android:inputType="number"/>

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



    mobilenumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {

                        mobilenumber.setAlpha(1);


                    } else {
                                        if (TextUtils.isEmpty(mobilenumber.getText().toString())) {
                            mobilenumber.setAlpha(0.3f);


                        }
                    }
                    mobilenumber.invalidate();
                }
            });

1 个答案:

答案 0 :(得分:0)

我认为问题在于你在呼唤:

mobilenumber.setAlpha(0.3f);

这样,您为整个视图设置了alpha。而不是为文本设置。

也许,您可以按如下方式更改代码:

mobilenumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // Fully opaque WHITE color
            v.setTextColor(Color.parseColor("#FFFFFFFF"));
        } else {
            // 70% transparent color
            if (TextUtils.isEmpty(mobilenumber.getText().toString())) {
                v.setTextColor(Color.parseColor("#55FFFFFF"));
            }
        }
        v.invalidate();
    }
});
相关问题