Android EditText 手机号344格式化输入的最佳实现

PhoneTextWatcher

手机号格式化监听器,支持普通输入/删除,中间输入/删除,在任意位置下黏贴/剪贴多个数字等多种交互场景。

目前支持的手机号格式为 3-4-4 分隔符可以自定义

Preview

在这里插入图片描述

How to get

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.jaydroid1024:PhoneTextWatcher:0.0.2'
}

How to use

val editTextSpace = findViewById<EditText>(R.id.editText_space)
val textViewSpace = findViewById<TextView>(R.id.textView_space)
// 缺省分隔符为空格
val phoneTextWatcherSpace = PhoneTextWatcher()
editTextSpace.addTextChangedListener(phoneTextWatcherSpace)
// 设置格式化输入的回调
phoneTextWatcherSpace.setTextChangedCallback(object : TextChangeCallback() {
    override fun afterTextChanged(s: String?, isPhoneNumberValid: Boolean) {
        textViewSpace.text = "反格式化后的手机号为:$s \n是否是有效的手机号:$isPhoneNumberValid"
    }
})

val editTextLine = findViewById<EditText>(R.id.editText_line)
val textViewLine = findViewById<TextView>(R.id.textView_line)
// 指定分隔符为横线,或者你传入的字符
val phoneTextWatcherLine = PhoneTextWatcher(AsYouTypeFormatter.SEPARATOR_LINE)
editTextLine.addTextChangedListener(phoneTextWatcherLine)
// 设置格式化输入的回调
phoneTextWatcherLine.setTextChangedCallback(object : TextChangeCallback() {
    override fun afterTextChanged(s: String?, isPhoneNumberValid: Boolean) {
        textViewLine.text = "反格式化后的手机号为:$s \n是否是有效的手机号:$isPhoneNumberValid"
    }
})

Go to

Github

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jaydroid

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值