工具类如下
/**
* View 的定义属性
* */
object ViewAdapter {
/**
* 用于切换背景选中状态的颜色
* */
@BindingAdapter(
value = ["isViewCheck", "viewCheckResource", "viewUnCheckResource"],
requireAll = false
)
@JvmStatic
fun viewBackgroundCheckChange(
view: View,
isViewCheck: Boolean = true,
viewCheckResource: Int?,
viewUnCheckResource: Int?
) {
var viewCheck = viewCheckResource ?: R.mipmap.cb_blue_true
var viewUnCheck = viewUnCheckResource ?: R.mipmap.cb_blue_false
if (view is ImageView) {
view.setImageResource(if (isViewCheck) viewCheck else viewUnCheck)
} else
view.setBackgroundResource(if (isViewCheck) viewCheck else viewUnCheck)
}
/**
* 用于切换背景选中状态的颜色
* */
@SuppressLint("ResourceAsColor")
@BindingAdapter(
value = ["isTextViewCheck", "checkTextColor", "unCheckTextColor"],
requireAll = false
)
@JvmStatic
fun textColorCheckChange(
view: TextView,
isTextViewCheck: Boolean = true,
@ColorInt checkTextColor: Int? = null,
@ColorInt unCheckTextColor: Int? = null
) {
var checkText = checkTextColor ?: Color.parseColor("#333333")
var unCheckText = unCheckTextColor ?: Color.parseColor("#999999")
view.setTextColor(if (isTextViewCheck) checkText else unCheckText)
}
/**
* 用于切换背景选中状态的颜色
* */
@BindingAdapter(
value = ["textChange"],
requireAll = false
)
@JvmStatic
fun editTextChange(
view: TextView,
textChange: (String) -> Unit
) {
view.addTextChangedListener {
textChange(it.toString())
}
}
// @BindingAdapter(
// value = ["pwdtextChange"],
// requireAll = false
// )
// @JvmStatic
// fun editTextChange(
// view: TextView,
// textChange: (String,Int) -> Unit
// ) {
// view.addTextChangedListener {
// textChange(it.toString(),view.id)
// }
// }
/**
* 只能适用于同一级且在同一父布局
* 清空TextView 的值
*
* @param view
* @param editTextViewId
*/
@BindingAdapter(
value =["editTextViewId"],
requireAll = false
)
fun clearEditTextView(view: View, @IdRes editTextViewId: Int) {
val parent = view.rootView
val text: TextView = parent.findViewById(editTextViewId)
if (text != null) {
view.setOnClickListener { v: View? ->
text.text = ""
}
}
}
@BindingAdapter("editTextId",
requireAll = false
)
@JvmStatic
fun eyeChange(box: CheckBox, @IdRes editTextId: Int) {
val parent = box.rootView
val editText = parent.findViewById(editTextId) as EditText
// editText.filters = null
// editText.filters = arrayOf(object : InputFilter {
// override fun filter(source: CharSequence?, start: Int, end: Int, dest: Spanned?, dstart: Int, dend: Int): CharSequence? {
// val regex = "^\u4E00-\u9FA5+$"
// val isChinese = Regex.matches(regex, source?.toString())
// if (!Character.isLetterOrDigit(source!!.get(start)) || isChinese) {
// return ""
// }
// return null
// }
// })
box.setOnCheckedChangeListener { buttonView, isChecked ->
val checked = box.isChecked
if (checked) {
// editText.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
editText.transformationMethod = HideReturnsTransformationMethod.getInstance()
} else {
// editText.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
editText.transformationMethod = PasswordTransformationMethod.getInstance()
}
editText.setSelection(editText.text.toString().length)
}
}
/**
* 用于切换背景选中状态的颜色
* */
@SuppressLint("SetTextI18n")
@BindingAdapter(
value = ["textStr", "startCount", "tagText", "endCount"],
requireAll = false
)
@JvmStatic
fun hideText(
view: TextView,
textStr: String? = "",
startCount: Int,
tagText: String? = "***",
endCount: Int
) {
view.text = textStr ?: ""
val text = view.text
text.i("hideText")
if (text.length > endCount)
view.text = text.substring(0, startCount) + tagText + text.substring(
text.length - endCount,
text.length
)
}
/**
* 短信验证码输入框
* */
@BindingAdapter(
value = ["beforeInputId", "inputId", "afterInputId", "inputCallBack", "onPaste"],
requireAll = false
)
@JvmStatic
fun inputCodeView(
view: EditText,
@IdRes beforeInputId: Int,
@IdRes inputId: Int,
@IdRes afterInputId: Int,
inputCallBack: ((String?, Boolean) -> Unit)? = { _, _ -> },
onPaste: ((View, String) -> Unit)? = { _, _ -> false }
) {
val parent = view.parent as View
var inputView: EditText? = null
if (inputId != null) {
inputView = parent.findViewById(inputId)
}
var beforeInput: EditText? = null
if (beforeInputId != null) {
beforeInput = parent.findViewById(beforeInputId)
}
var afterInput: EditText? = null
if (afterInputId != null) {
afterInput = parent.findViewById(afterInputId)
}
inputView?.addTextChangedListener {
if (it.toString().length > 1) {
it.i("PhoneCodeFragment inputView ")
onPaste?.invoke(inputView, it.toString())
return@addTextChangedListener
}
if (!it.isNullOrBlank() && !it.isNullOrEmpty()) {
afterInput?.requestFocus()
if (afterInput == null) {
inputCallBack?.invoke(it.toString(), true)
} else {
inputCallBack?.invoke("", false)
}
} else {
beforeInput?.requestFocus()
inputCallBack?.invoke("", false)
}
inputView.setSelection(inputView.text.length)
}
}
}