在android新推出的Material Design中对文本输入框的样式提供了标准,并且在AppCompat v21 中提供了Material Design的空间外观支持,如下图:
不过该控件在使用的过程中比较繁琐,需要通过为控件定制theme的方式来实现自定义控件颜色,并且并没有提供Material Design中提到的特性,因此,为了使用实现该特性效果,我们可以使用rengwuxian在github推出的库MaterialEditText.来实现
使用方法:
首先引用库
compile ‘com.rengwuxian.materialedittext:library:2.1.4’
"match_parent"
android:layout_height="wrap_content"
android:hint="Basic"/>
修改输入的字体颜色
app:met_baseColor="#0056d3"
app:met_primaryColor="#982360"
app:met_floatingLabel="normal"
app:met_floatingLabel="highlight"
app:met_floatingLabelText="XXX"
met_floatingLabelText方法需要配合上面两个参数才能生效
"match_parent"
android:layout_height="wrap_content"
app:met_baseColor="#0056d3"
app:met_primaryColor="#982360"
app:met_floatingLabelText="aaaaaa"
app:met_floatingLabel="normal"
android:hint="Basic"/>
当设置底部省略号后,控件会在文字向左滚动时,在底部显示一个三个点的胜率号,这个方法会自动将android:singleLine设置为true
app:met_singleLineEllipsis="true"
设置字符数限制后,控件右下角会显示已输入字符数和最大字符数的角标,并在超过限制后显示警告色(默认为红色).
//设置警告色:
app:met_errorColor="#000000"
//设置最多字数和最小字数
app:met_minCharacters="5"
app:met_maxCharacters="10"
app:met_helperText="Integer"
Error Text需要动态调用
setError(CharSequence error)
validationEditText.isValid("\\d+");
validationEditText.validate("\\d+", "Only Integer Valid!");
app:met_accentTypeface="fonts/Roboto-LightItalic.ttf"
Hide UnderLine
删除默认的下划线
app:met_hideUnderline="true"
检查后,如果有错,将自动调用 setError() 方法来提示错误。
单一条件检查:
et.validateWith(new RegexpValidator("Only Integer Valid!", "\\d+"));
复合条件检查:
et.addValidator(new CustomValidator1())
.addValidator(new CustomValidator2())
.addValidator(new RegexpValidator("Only Integer Valid!", "\\d+"));
在输入框左右放置图片,默认图片和输入框之间的距离为16dp
可以通过met_iconPadding设置
app:met_iconLeft="@mipmap/ic_launcher"
app:met_iconRight="@mipmap/ic_launcher"
显示清空输入框btn
app:met_clearButton= “true”