MaterialEditText的使用:输入框

MaterialEditText github地址

参考:

MaterialEditText库使用详解
Android MaterialEditText
分享一个GitHub 上的控件:MaterialEditText 详解
Android 修改EditText的光标颜色和背景色

通用颜色

met_baseColor: 底部横线和所有文字在无焦点状态的基础色。默认为黑色。
met_primaryColor: 底部横线和 Floating label 的高亮色(如果 met_floatingLabel 设置为 highlight 的话)。默认使用 baseColor。
met_textColor: 和自带的 android:textColor 作用相同。换用这个就好。
met_textColorHint: 和自带的 android:textColorHint 作用相同。换用这个就好。
met_underlineColor: 自定义底部横线的颜色。

-Floating label

met_floatingLabel: Floating label 应该怎样被展示。选项有:none, normal, highlight。 默认是 none.
met_floatingLabelText: 自定义 floating label 的文字。
met_floatingLabelTextSize: Floating label 的字体大小。默认为 12sp。
met_floatingLabelTextColor: Floating label 的字体颜色。默认为半透明的 baseColor.
met_floatingLabelPadding: Floating label 和主文字区域的间隔。
met_floatingLabelAnimating: 是否使用动画来显示和消失 floating label 。默认为 true 。
met_floatingLabelAlwaysShown: 是否总是显示 Floating label 。默认为 false 。

字数限制

met_minCharacters: 限制的最少字数。默认为0。
met_maxCharacters: 限制的最大字数。0为无限制。默认为0.
-Helper/Error text
met_helperText: 底部的 helper text。
met_helperTextAlwaysShown: 是否总是显示 helper text, 而不仅仅是在获得焦点状态时。默认为 false。
met_helperTextColor: Helper text 的字体颜色。
met_errorColor: Error text 的字体颜色。
met_bottomTextSize: 底部文字(Helper/Error text)的字体大小。默认为12sp。
met_minBottomTextLines:底部为文字预留的行数,不管是否有 Helper/Error text 正在显示。

字体(Typeface)

met_typeface: 主文字的字体.
met_accentTypeface: 辅助文字的字体.
Material Design 风格的左右图标
met_iconLeft: 左边的图标.
met_iconRight: 右边的图标.
met_iconPadding: 图标和主区域之间的padding。默认为16dp, 遵守 Google’s Material Design Spec 中的建议.

Others

met_hideUnderline: 是否隐藏底部横线。默认为 false。
met_autoValidate: 是否自动检查字符串。默认为 false。
met_singleLineEllipsis: 是否在文字超长时显示底部的省略号。默认为 false。
met_clearButton: 是否显示用来清空文字的 Clear button 。默认为 false。

示例:

导入依赖

compile 'com.rengwuxian.materialedittext:library:2.1.4'

布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="50dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="50dp"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--基本使用-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="基本使用" />

        <!--修改在无焦点状态底部横线和所有文字的基础色。默认为黑色。-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="修改下划线和文字在无焦点状态颜色"
            app:met_baseColor="#0000ff" />

        <!--修改焦点状态底部横线和 Floating label 的高亮色-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="修改底部横线和Floating label的高亮色"
            app:met_primaryColor="#0000ff" />

        <!--修改无焦点底部横线的颜色-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="修改底部横线的颜色"
            app:met_underlineColor="#0000ff" />

        <!--Floating Label-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="met_floatingLabel=none"
            app:met_floatingLabel="none"
            app:met_floatingLabelText="请输入信息" />

        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="met_floatingLabel=normal"
            app:met_floatingLabel="normal"
            app:met_floatingLabelText="请输入信息" />

        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="met_floatingLabel=highlight"
            app:met_floatingLabel="highlight"
            app:met_floatingLabelText="请输入信息" />
        <!--Floating Label-->

        <!--底部省略号-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="底部省略号"
            app:met_singleLineEllipsis="true" />

        <!--字符数限制-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="字符数限制"
            app:met_errorColor="#ff0000"
            app:met_maxCharacters="10"
            app:met_minCharacters="5" />

        <!--删除默认的下划线-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="删除默认的下划线"
            app:met_hideUnderline="true" />

        <!--显示清空输入框按钮-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="显示清空输入框按钮"
            app:met_clearButton="true" />

        <!--met_helperText-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="met_helperText"
            app:met_helperText="Integer" />

        <!--字数限制-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="字数限制"
            app:met_errorColor="#000000"
            app:met_maxCharacters="10"
            app:met_minCharacters="5" />

        <!--放置图片:在输入框左右放置图片,默认图片和输入框之间的距离为16dp -->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="放置图片"
            app:met_iconLeft="@drawable/ic_launcher"
            app:met_iconPadding="20dp" />

        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="放置图片"
            app:met_iconPadding="20dp"
            app:met_iconRight="@drawable/ic_launcher" />
        <!--放置图片-->

        <!--改变光标颜色-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="改变光标颜色"
            android:textCursorDrawable="@drawable/edit_cursor_color" />

        <!--综合效果推荐-->
        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/et1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入手机号码"
            android:inputType="phone"
            android:textCursorDrawable="@drawable/edit_cursor_color"
            app:met_clearButton="true"
            app:met_errorColor="#ff0000"
            app:met_floatingLabel="normal"
            app:met_floatingLabelText="请输入手机号码"
            app:met_helperText="电话号码"
            app:met_maxCharacters="11"
            app:met_primaryColor="#ff8041"
            app:met_textColor="#000000" />

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="提交" />
    </LinearLayout>

</ScrollView>

简介:

//修改光标
 android:textCursorDrawable="@drawable/edit_cursor_color"

//添加删除按钮
app:met_clearButton="true"

//左上角提示:输入
app:met_floatingLabel="normal"
app:met_floatingLabelText="请输入手机号码"

//左下角提示:正常还是错误
app:met_helperText="电话号码"

//错误后下划线和字数提示的颜色
app:met_errorColor="#ff0000"

//最多输入多少字
app:met_maxCharacters="11"

//输入文本颜色
app:met_textColor="#000000"

//底部横线和 Floating label 的高亮色
app:met_primaryColor="#ff8041"

drawable/edit_cursor_color:光标:颜色、粗细的修改

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="2dp" />
    <solid android:color="#ff8041" />
</shape>

手机号码验证:

public class PhoneValidator extends METValidator {

    private Pattern pattern;

    public PhoneValidator(@NonNull String errorMessage) {
        super(errorMessage);
    }

    public PhoneValidator(@NonNull String errorMessage, @NonNull String regex) {
        super(errorMessage);
        pattern = Pattern.compile(regex);
    }

    public PhoneValidator(@NonNull String errorMessage, @NonNull Pattern pattern) {
        super(errorMessage);
        this.pattern = pattern;
    }


    @Override
    public boolean isValid(@NonNull CharSequence text, boolean isEmpty) {
        return pattern.matcher(text).matches();
    }


}

使用:

public class MyActivityI extends AppCompatActivity {
    private MaterialEditText et1;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edittext);

        et1 = ((MaterialEditText) this.findViewById(R.id.et1));
        btn = ((Button) this.findViewById(R.id.btn));
        //添加校验规则
        et1.addValidator(new RegexpValidator("电话号码不正确", "^0?1[3458]\\d{9}$"));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et1.validate();//执行校验
            }
        });
    }
}

不定义METValidator 的写法(已废弃)

        et1 = ((MaterialEditText) this.findViewById(R.id.et1));
        btn = ((Button) this.findViewById(R.id.btn));
//        et1.addValidator(new RegexpValidator("电话号码不正确", "^0?1[3458]\\d{9}$"));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                et1.validate();
                et1.validate("^0?1[3458]\\d{9}$", "电话号码不正确");//已废弃
            }
        });

效果图:

这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值