我想为动态而不是编辑文本设置一个文本观察器.如果我正在创建动态编辑文本,我想对所有动态编辑文本使用一个文本观察器.如果有人知道告诉我
您可以为编辑文本创建自定义文本观察程序类
class DynamicTextWatcher(private val editText: EditText, private val type: String) :
TextWatcher {
var beforeText : String = ""
override fun beforeTextChanged(p0: CharSequence?, start: Int, before: Int, count: Int) {
beforeText = p0.toString()
}
override fun onTextChanged(p0: CharSequence?, start: Int, before: Int, count: Int) {
if (p0 == null)
return
// 1. get cursor position : p0 = start + before
val initialCursorPosition = start + before
//2. get digit count after cursor position : c0
val numOfDigitsToRightOfCursor = getNumberOfDigits(beforeText.substring(initialCursorPosition,
beforeText.length))
try {
editText.removeTextChangedListener(this)
val value = editText.text.toString().trimStart(' ')
if (value != "") {
when(type){
Constants.FORM_INPUT_NAME -> {
//editText.setText(value.replaceFirstChar { it.uppercase() })
val fc = value[0]
if (fc == ' '){
editText.setText(value.drop(1))
}else{
if (value.contains(" ")){
editText.setText(value.replace(" ", " "))
}
else {
editText.setText(value)
}
}
}
Constants.FORM_INPUT_NO_SPACE -> editText.setText(value.removeWhitespaces())
Constants.FORM_INPUT_MOBILE -> {
val cs = value.removeWhitespaces()
val cleanString = cs.replace("[^0-9]".toRegex(), "")
editText.setText(cleanString)
}
//Constants.FORM_INPUT_PASSWORD -> editText.setText(value.removeWhitespaces())
}
}else{
editText.setText(value)
}
editText.setSelection(getNewCursorPosition(numOfDigitsToRightOfCursor, value,type))
editText.addTextChangedListener(this)
} catch (ex: Exception) {
ex.printStackTrace()
editText.addTextChangedListener(this)
}
}
override fun afterTextChanged(s: Editable) {
}
private fun getNewCursorPosition(digitCountToRightOfCursor : Int, numberString : String, type : String) : Int{
Log.v("getNewCursorPosition","hello"+numberString+"hello")
var position = 0
var c = digitCountToRightOfCursor
var value = numberString
when(type){
Constants.FORM_INPUT_NAME -> {
if (numberString.contains(" ")){
value = numberString.replace(" ", " ")
}
}
Constants.FORM_INPUT_NO_SPACE -> value = numberString.removeWhitespaces()
Constants.FORM_INPUT_MOBILE -> { }
}
for (i in value.reversed()) {
if (c == 0)
break
else
c --
position ++
}
return value.length - position
}
private fun getNumberOfDigits(@NonNull text : String) : Int{
var count = 0
for (i in text)
count++
return count
}
}
活动中显示的代码
registerSheetBinding.etPassword.addTextChangedListener(
DynamicTextWatcher(
registerSheetBinding.etPassword,
Constants.FORM_INPUT_NO_SPACE
)
)
registerSheetBinding.etEmail.addTextChangedListener(
DynamicTextWatcher(
registerSheetBinding.etEmail,
Constants.FORM_INPUT_NO_SPACE
)
)
registerSheetBinding.etFullName.addTextChangedListener(
DynamicTextWatcher(
registerSheetBinding.etFullName,
Constants.FORM_INPUT_NAME
)
)
我希望这段代码对您有所帮助。#Android 开发者
您可以
创建自定义的TextWatcher类,如下所示。
private class CustomTextWatcher implements TextWatcher{
private EditText et;
private CustomTextWatcher (EditText et){
this.et = et;
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
要设置文本观察器,您可以执行以下操作
yourdynamicedittext.addTextChangedListener(new CustomTextWatcher(yourdynamicedittext));
在 CustomTextWatcher 类中,您可以处理分配给 TextChangedListener 的每个编辑文本的相对大小写。为了识别不同的编辑文本,您可以在CustomTextWatcher类中设置标签并检索它。
yourdynamicedittext.setTag(1, 2, ...n);