[我的活动有一个editText,我想在运行中检查输入的文本,如果文本等于“ 1”,那么事情将会改变..如何在Kotlin中做到这一点?
fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
textView9.visibility = View.GONE
spin7.visibility = View.GONE
spin8.visibility = View.GONE
spin9.visibility = View.GONE
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
val Edit = editText.text.toString()
if (Edit.equals("1")){
textView9.visibility = View.GONE
spin7.visibility = View.GONE
spin8.visibility = View.GONE
spin9.visibility = View.GONE
}
else {
textView9.visibility = View.VISIBLE
spin7.visibility = View.VISIBLE
spin8.visibility = View.VISIBLE
spin9.visibility = View.VISIBLE
}
}
override fun afterTextChanged(editable: Editable?) {
}
})
}
尝试一下:
yourEditText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
val text = p0.toString()
if (text == "1") {
//do something
} else {
//do something else
}
}
})
您可以在这里看到方法的工作方式(source):