美文网首页
自定义EditText设置底部下划线在各种状态下颜色改变

自定义EditText设置底部下划线在各种状态下颜色改变

作者: nicegoing | 来源:发表于2016-06-20 08:48 被阅读7459次

项目中需要实现正常情况下一种默认色彩,获取焦点后下划线变色,如果失去焦点,检验输入值改变颜色。我们知道下划线是EditText的background,那么可以给它一个background并设置在不同的状态下改变background的值。

background使用shape绘制
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:id="@+id/shape"
        android:bottom="1dp"
        android:left="-10dp"
        android:right="-10dp"
        android:top="-10dp">
        <shape>
            <solid android:color="@android:color/transparent"/>
            <stroke android:width="1dp" android:color="@color/color_f1f1f1"/>
        </shape>
    </item>
</layer-list>

EditText
public class AddressEditText extends EditText implements View.OnFocusChangeListener {
    private Context context;


    public void setOnCheckInputListener(OnCheckInputListener onCheckInputListener) {
        this.onCheckInputListener = onCheckInputListener;
    }

    private OnCheckInputListener onCheckInputListener;
    private GradientDrawable     drawable;

    /**
     * 检测输入是否符合要求的回调
     */
    public interface OnCheckInputListener {
        /**
         * 检测输入的方法
         *
         * @param v   点击的view
         * @param str 输入的字符串
         * @return 检测成功返回true, 检测失败返回false
         */
        boolean checkInput(View v, String str);
    }

    public AddressEditText(Context context) {
        this(context, null);
    }

    public AddressEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayerDrawable layerDrawable = (LayerDrawable) getBackground();
        drawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.shape);
        setOnFocusChangeListener(this);
    }


    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            LogUtil.i("获取焦点");
            drawable.setStroke(ResourceUtil.getDimens(R.dimen.dp_1), ResourceUtil.getColor(R.color.color_d1d1d1));
        } else {
            LogUtil.i("失去焦点");

            if (onCheckInputListener != null && onCheckInputListener.checkInput(this, getText().toString().trim())) {
                drawable.setStroke(ResourceUtil.getDimens(R.dimen.dp_1), ResourceUtil.getColor(R.color.color_f1f1f1));

            } else if (onCheckInputListener == null) {
                drawable.setStroke(ResourceUtil.getDimens(R.dimen.dp_1), ResourceUtil.getColor(R.color.color_f1f1f1));
            } else {
                drawable.setStroke(ResourceUtil.getDimens(R.dimen.dp_1), ResourceUtil.getColor(R.color.color_ff6f00));

            }
        }
    }
}

相关文章

  • 自定义EditText设置底部下划线在各种状态下颜色改变

    项目中需要实现正常情况下一种默认色彩,获取焦点后下划线变色,如果失去焦点,检验输入值改变颜色。我们知道下划线是Ed...

  • 继承特定的view完成自定义控件

    自定义控件功能说明 设置使用自定义的字体 在EditText获得输入焦点的时候,下划线变为黄色,否则采用自己设置的...

  • Edittext相关

    更改下划线的颜色 普通来说,对当前页面/window设置主题,主题里设置colorAccent就是Edittext...

  • Android中的EditText的一些常用属性设置

    EditText去掉下划线和边框 将EditText的backgroud属性值设为@null 设置EditText...

  • 自定义UISegmentView类

    自定义UISegmentView类,可以设置item数量,选中item颜色,背景视图颜色,下划线颜色和高度,选中i...

  • Android设置EditText光标颜色

    Android EditText的光标颜色主要是这个属性设置的,可以更改这个属性来改变游标的颜色

  • EditText去掉下划线

    EditText去掉下划线想做个没下划线的EditText,刚开始比较单纯想找个属性直接设置不显示得了,结果发现没...

  • EditText

    关于EditText的设置 自定义输入内容布局文件中设置digits属性,值就是你的自定义内容

  • BUG 记录

    EditText 光标 textCursorDrawablea. 必须设置图片b. 必须设置大小 下划线andr...

  • 改变tabbarItem选中颜色

    改变tabbarItem选中颜色 tabBar中tabBarItem选中颜色自定义设置 直接代码设置 tabBar...

网友评论

      本文标题:自定义EditText设置底部下划线在各种状态下颜色改变

      本文链接:https://www.haomeiwen.com/subject/nfdwdttx.html