PhoneTextWatcher
手機(jī)號格式化監(jiān)聽器赋兵,支持普通輸入/刪除,中間輸入/刪除霹期,在任意位置下黏貼/剪貼多個數(shù)字等多種交互場景。
目前支持的手機(jī)號格式為 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)
// 設(shè)置格式化輸入的回調(diào)
phoneTextWatcherSpace.setTextChangedCallback(object : TextChangeCallback() {
override fun afterTextChanged(s: String?, isPhoneNumberValid: Boolean) {
textViewSpace.text = "反格式化后的手機(jī)號為:$s \n是否是有效的手機(jī)號:$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)
// 設(shè)置格式化輸入的回調(diào)
phoneTextWatcherLine.setTextChangedCallback(object : TextChangeCallback() {
override fun afterTextChanged(s: String?, isPhoneNumberValid: Boolean) {
textViewLine.text = "反格式化后的手機(jī)號為:$s \n是否是有效的手機(jī)號:$isPhoneNumberValid"
}
})