当我在addTextChangedListener(textWacther)上调用EditText并尝试运行该应用程序时,当在该字段中输入内容时,它将关闭。
我启用按钮后退的方式不是正确的吗?

class MainActivity : AppCompatActivity() {
lateinit var num1TextE: EditText
lateinit var resultText: TextView
lateinit var plusBtn: Button

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    num1TextE = findViewById(R.id.firstNum_TextE)
    plusBtn = findViewById(R.id.plus_btn)
    resultText = findViewById(R.id.result_TextV)
    resultText.visibility = View.GONE

    plusBtn.isClickable= false
    plusBtn.isEnabled = false
    num1TextE.addTextChangedListener(textWatcher)

    plusBtn.setOnClickListener {
        var num1 = num1TextE.text.toString().toInt()
        var num2 = num2TextE.text.toString().toInt()
        resultText.visibility = View.VISIBLE
        resultText.text = "Result is ${num1 + num2}"
   }
}

var textWatcher = object : TextWatcher {
    override fun afterTextChanged(p0: Editable?) {

    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        var test: String = num1TextE.text.toString()
        if(! test.isEmpty()) {
            plusBtn.isClickable = true
            plusBtn.isEnabled = true
        }
       }
    }
}

日志:
2019-07-31 17:02:01.633 3078-3078/com.example.mycalculator E/InputEventSender: Exception dispatching finished signal.
2019-07-31 17:02:01.633 3078-3078/com.example.mycalculator E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-07-31 17:02:01.637 3078-3078/com.example.mycalculator E/MessageQueue-JNI: kotlin.NotImplementedError: An operation is not implemented: not implemented
    at com.example.mycalculator.MainActivity$textWatcher$1.beforeTextChanged(MainActivity.kt:101)

最佳答案

如果不打算使用回调,请删除TODO("not implemented")并将beforeTextChanged() { }留空。

07-26 01:12