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) { // 文字改变时 } override fun afterTextChanged(s: Editable) { // 修改完成后 } })
editText.onFocusChangeListener = object : View.OnFocusChangeListener { override fun onFocusChange(v: View, hasFocus: Boolean) { if (hasFocus) { // 获得焦点时 } else { // 失去焦点时 } } }
editText.setOnEditorActionListener { v, actionId, event -> when (actionId) { EditorInfo.IME_ACTION_DONE -> { // 按下“完成”键 true } EditorInfo.IME_ACTION_SEARCH -> { // 按下“搜索”键 true } EditorInfo.IME_ACTION_GO -> { // 按下“前往”键 true } else -> false } }