• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

FormatEditText: A edittext that can use the specified rule, 自定义规则格式化号码 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

FormatEditText

开源软件地址:

https://gitee.com/dkzwm/FormatEditText

开源软件介绍:

FormatEditText

English | 中文

本库提供3个类以供使用:

  • ClearEditText 可以用来当带清除功能的文本输入框使用.
  • FormattedEditText 可以用来当做格式化文本输入框使用,支持任意字符按指定规则进行格式化.
  • MaskNumberEditText 可以用来当做数字或金额文本输入框使用.

特性:

  • 支持配置格式化样式
  • 支持配置输入提示
  • 支持粘贴且光标自动跟随
  • 自动填充删除占位符
  • 支持配置清除图标且不会占用CompoundDrawables的位置

演示程序

下载 Demo.apk

快照

引入

添加如下依赖到你的 build.gradle 文件:

repositories {    ...    maven { url 'https://jitpack.io' }}dependencies {    implementation 'com.github.dkzwm:FormatEditText:0.2.0'}

使用

在Xml中配置

<me.dkzwm.widget.fet.FormattedEditText    android:id="@+id/formattedEditText_simple"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:inputType="phone"    android:maxLength="13"    app:fet_clearDrawable="@drawable/icon_clear"    app:fet_drawableGravity="fet_center"    app:fet_drawablePadding="4dp"    app:fet_formatStyle="344"    app:fet_mode="mode_simple"    app:fet_placeholder="-" /><me.dkzwm.widget.fet.FormattedEditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:inputType="phone"    android:maxLength="19"    app:fet_clearDrawable="@drawable/icon_clear"    app:fet_drawableGravity="fet_center"    app:fet_drawablePadding="4dp"    app:fet_formatStyle="+(86)-***-****-****"    app:fet_mark="*"    app:fet_mode="mode_complex" /><me.dkzwm.widget.fet.FormattedEditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:inputType="phone"    android:maxLength="19"    app:fet_clearDrawable="@drawable/icon_clear"    app:fet_drawableGravity="fet_center"    app:fet_drawablePadding="4dp"    app:fet_emptyPlaceholder="_"    app:fet_formatStyle="+(86)-000-0000-0000"    app:fet_mode="mode_mask"    app:fet_showHintWhileEmpty="true" /><me.dkzwm.widget.fet.FormattedEditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:digits="0123456789X"    android:maxLength="22"    app:fet_clearDrawable="@drawable/icon_clear"    app:fet_drawableGravity="fet_center"    app:fet_drawablePadding="4dp"    app:fet_formatStyle="000 000 0000 0000 000*"    app:fet_hintText="100 000 2020 0101 000X"    app:fet_mode="mode_hint"    app:fet_showHintWhileEmpty="false" /><me.dkzwm.widget.fet.MaskNumberEditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    app:fet_clearDrawable="@drawable/icon_clear"    app:fet_drawableGravity="fet_center"    app:fet_drawablePadding="4dp"    app:fet_autoFillNumbers="false"    app:fet_currencySymbol="¥"    app:fet_decimalLength="2"    app:fet_showThousandsSeparator="true" />

Java代码配置

FormattedEditText.Config.create()        .mode(FormattedEditText.MODE_SIMPLE) //简单模式        .formatStyle("344") //格式化样式        .placeholder(' ') //占位符        .config(editText);FormattedEditText.Config.create()        .mode(FormattedEditText.MODE_COMPLEX) //组合模式        .formatStyle("+(86)-***-****-****") //格式化样式        .mark('*') //标记符        .config(editText);FormattedEditText.Config.create()        .mode(FormattedEditText.MODE_MASK) //掩码匹配模式        .formatStyle("+(86)-000-0000-0000") //格式化样式        .emptyPlaceholder('_') //空白数据占位符        .showHintWhileEmpty(true) //是否清除数据后显示默认提示文字(android:hint),开启后不会用空白数据占位符填充数据位        .config(editText);FormattedEditText.Config.create()        .mode(FormattedEditText.MODE_HINT) //提示模式        .formatStyle("000 000 0000 0000 000X") //格式化样式        .maskFilter("X", new FormattedEditText.Matcher() {            @Override            public boolean hasMatch(String previousText, String value) {                return TextUtils.isDigitsOnly(value) || value.toUpperCase().equals("X");            }        })//自定义掩码匹配        .maskFilter("0", new FormattedEditText.Matcher() {            @Override            public boolean hasMatch(String previousText, String value) {                return TextUtils.isDigitsOnly(value);            }        })//自定义掩码匹配        .hintText("100 000 2020 0101 000X") //提示文字,不同于默认提示文字(android:hint),会在输入字符后依然显示,必须和格式化样式格式一致        .hintColor(Color.GRAY) //提示文字颜色        .showHintWhileEmpty(true) //是否清除数据后显示默认提示文字(android:hint),开启后不会用空白数据占位符填充数据位        .config(editText);MaskNumberEditText editText = new MaskNumberEditText(context);editText.setShowThousandsSeparator(true);//是否显示千位分隔符`,`editText.setAutoFillNumbers(true);//是否自动填充小数,如设置小数保留2位,那么当位数不足时会自动填充`0`editText.setDecimalLength(2);//小数位长度editText.setCurrencySymbol("¥");//设置货币符号,不设置即不显示editText.setMaxNumberValue(6666.66)//指定能输入的最大数值

Xml属性

ClearEditText
名称类型描述
fet_clearDrawablereference指定删除图标
fet_drawableGravityenum指定删除图标的对齐方式,支持GRAVITY_TOPGRAVITY_CENTERGRAVITY_BOTTOM,默认为GRAVITY_CENTER,即居中对齐
fet_drawablePaddingdimension指定删除图标的填充大小
FormattedEditText
名称类型描述
fet_modeenum指定模式,支持MODE_SIMPLE(简单模式)、MODE_COMPLEX(组合模式)、MODE_MASK(掩码匹配模式)、MODE_HINT(提示模式)
fet_formatStylestring指定格式化样式,当fet_modeMODE_SIMPLE时,格式只能是纯数字, fet_modeMODE_COMPLEX时,格式为任意格式且需要指定fet_mark属性,如果不指定那么默认为*
fet_markstring指定标记符,仅当fet_modeMODE_COMPLEX时设置会起作用,且长度必须为1(默认:*
fet_placeholderstring指定占位符,仅当fet_modeMODE_SIMPLE时设置会起作用,且长度必须为1(默认:
fet_emptyPlaceholderstring指定空白占位符,仅当fet_modeMODE_MASK时设置会起作用,且长度必须为1(默认:
fet_hintTextstring指定提示文字,仅当fet_modeMODE_HINT时设置会起作用,必须和格式化样式格式一致
fet_hintTextColorcolor指定提示文字颜色,仅当fet_modeMODE_HINT时设置会起作用
fet_showHintWhileEmptyboolean指定是否清除数据后显示默认提示文字(android:hint),仅当fet_modeMODE_MASKMODE_HINT时设置会起作用
MaskNumberEditText
名称类型描述
fet_decimalLengthinteger指定小数位长度
fet_currencySymbolstring指定货币符号
fet_currencySymbolTextColorstring指定货币符号文字颜色,不设置的话默认使用当前文字颜色
fet_showThousandsSeparatorboolean指定是否显示千位分隔符
fet_autoFillNumbersboolean指定是否自动填充数字
fet_autoFillNumbersTextColorboolean指定自动填充数字的文字颜色,不设置的话默认使用当前提示文字颜色
fet_maxNumberValuestring指定能输入的最大数值

掩码

FormattedEditText 在模式为MODE_MASKMODE_HINT时,格式化样式中的以下字符具有特殊含义:

  • 0 - 数字掩码,只接受输入数字
  • A - 英文字母掩码,只接受输入英文字母
  • * - 数字和英文字母掩码,接受输入数字和英文字母
  • ? - 字符掩码,接受输入任何内容

样式中的其他字符则会原样显示。如果需要原样显示这4个特殊字符,则需要使用转义符\。例如\\0\\086 000 000 000在格式化时0086会原样显示。

感谢

License

--------	Copyright (c) 2017 dkzwmPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap