Android 在addTextChangedListener使应用程序挂起时,将editText文本设置为大写

Android 在addTextChangedListener使应用程序挂起时,将editText文本设置为大写,android,firebase,kotlin,android-edittext,Android,Firebase,Kotlin,Android Edittext,即使用户在edittext中写入小写,我也希望使用大写。因此,我将edittext文本在addTextChangedListener上设置为大写,如下所示 editText.addTextChangedListener(object : TextWatcher{ override fun beforeTextChanged(s : CharSequence?, start : Int, count : Int, after : Int) { }

即使用户在
edittext
中写入小写,我也希望使用大写。因此,我将
edittext
文本在
addTextChangedListener
上设置为大写,如下所示

  editText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s : CharSequence?, start : Int, count : Int, after : Int) {
            }

            override fun onTextChanged(s : CharSequence?, start : Int, before : Int, count : Int) {
                    editText.setText(s.toString().toUpperCase())
            }

            override fun afterTextChanged(s : Editable?) {

            }
    })

但是,当我在
editText
中键入内容时,执行此操作,应用程序正在挂起,我必须关闭应用程序并重新启动它。因为我想在绑定适配器中使用它,所以我不能使用xml的AllCaps方法。在编辑
EditText
的文本之前,您需要删除
textwatcher
,并在更改完成后再次读取它

    val watcher: TextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            editText.removeTextChangedListener(this)
            editText.setText(s.toString().toUpperCase())
            editText.addTextChangedListener(this)
        }

        override fun afterTextChanged(s: Editable) {}
    }

    editText.addTextChangedListener(watcher)

您可以在xml中使用输入类型,也可以通过编程方式使用输入类型

editText.inputType == InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
在xml中,您可以将

android:inputType="textCapCharacters"

此解决方案将自动将输入转换为UperCase

为什么不使用方法并为其指定textCapCharacters?这是否回答了您的问题?欢迎来到Stackoverflow。我看到你的问题中有一个坚实的问题陈述,但是如果你想得到答案,你的问题仍然需要一些改变。请查看并适当更新您的问题。是的,但我想在绑定适配器中添加此方法