Android 键入时未调用TextInputText addTextChangedListener
我有一个带有一个Android 键入时未调用TextInputText addTextChangedListener,android,kotlin,material-design,Android,Kotlin,Material Design,我有一个带有一个textinputtext(材质设计组件)的简单表单,无法侦听文本更改。使用addTextChangedListener添加TextWatcher似乎没有任何效果。当我运行我的应用程序并使用虚拟键盘输入时,不会调用任何回调(不会命中调试断点),并且我的TextView也不会更新: 活动: class MainActivity : AppCompatActivity() { private lateinit var webBankingIdLayout: TextInp
textinputtext
(材质设计组件)的简单表单,无法侦听文本更改。使用addTextChangedListener
添加TextWatcher
似乎没有任何效果。当我运行我的应用程序并使用虚拟键盘输入时,不会调用任何回调(不会命中调试断点),并且我的TextView
也不会更新:
活动:
class MainActivity : AppCompatActivity() {
private lateinit var webBankingIdLayout: TextInputLayout
private lateinit var webBankingIdInput: TextInputEditText
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webBankingIdLayout = findViewById<TextInputLayout>(R.id.web_banking_id_input_layout)
webBankingIdInput = TextInputEditText(webBankingIdLayout.context)
textView = findViewById<TextView>(R.id.temp_text)
webBankingIdInput.addTextChangedListener(object: TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
s!!
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
s!!
textView.text = s!!
}
override fun afterTextChanged(s: Editable?) {
s!!
}
})
}
}
为什么我的代码不起作用,如何在文本输入中收听字符输入?
我的目标是隐藏一个按钮,直到在字段中输入最少的字符数。您需要替换:
webBankingIdInput = TextInputEditText(webBankingIdLayout.context)
致:
webBankingIdInput=findViewById(R.id.web\u banking\u id\u text\u输入)
我也遇到了类似的问题,原因是没有在XML中设置android:inputType参数。有道理。我猜我假设将上下文传递给构造函数将返回布局中定义的实例。我猜我错了!
webBankingIdInput.text = "Yippee!"
webBankingIdInput = TextInputEditText(webBankingIdLayout.context)
webBankingIdInput = findViewById<TextInputEditText>(R.id.web_banking_id_text_input)