사용자에게 문자열 데이터를 입력을 받을 때 사용하는 View
// 입력 감시자 설정
val editTextWatcher1 = EditTextWatcher1()
binding.et.addTextChangedListener(editTextWatcher1)
// EditText 입력 감시자
inner class EditTextWatcher1 : TextWatcher {
// 입력 내용 변경 전
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
binding.textView.text = "before : $s"
}
// 입력 내용 변경 시
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
binding.textView2.text = "changed : $s"
}
// 입력 내용 변경 후
override fun afterTextChanged(s: Editable?) {
binding.textView3.text = "after : $s"
}
}
보통 실시간으로 사용자의 입력 내용을 받을 때 사용하는데 addTextChangedListener를 사용할 때 고차함수를 사용하면 TextWatcher의 after 역할을 수행한다.
binding.et.addTextChangedListener {
binding.textView.text = it
}
이게 훨씬 간단하다.
binding.et.setOnEditorActionListener { v, actionId, event ->
// 동작할 코드
// true: 엔터키를 누른 후에 포커스가 유지
// false: 엔터키를 누른 후에 다음 editText로 포커스 이동
true
}
반환값이 true이면, 엔터키를 누른 후에 포커스가 유지되고
반환값이 false이면, 엔터키를 누른 후에 다음 editText로 포커스 이동된다.
binding.et.requestFocus()
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(currentFocus, 0)
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
if (currentFocus != null) {
// currentFocus : 현재 포커스를 가지고 있는 view를 지칭할 수 있다.
imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
// 포커스 해제
currentFocus!!.clearFocus()
}