1. EditText 입력 후 배경 터치시 포커스 해제 및 키보드 숨김처리
// MARK: - EditText 입력 후 배경터치시 포커스 해제 및 키보드 숨김처리
override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
if (event?.action === MotionEvent.ACTION_DOWN) {
val v = currentFocus
// 특정 EditText 별도 처리시 id 값으로 구분
// DLog.e(Exception(), "dispatchTouchEvent1 : ${v} / ${v?.id}")
// v?.let {
// if ( it.id == R.id.etRrnSecond || it.id == R.id.etLicenseNumber3) {
// DLog.d(Exception(), "etRrnSecond or etLicenseNumber3")
// return false
// }
// }
if (v is EditText) {
val outRect = Rect()
v.getGlobalVisibleRect(outRect)
// 배경터치시 키보드 숨김 처리
if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
v.clearFocus()
val imm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(v.getWindowToken(), 0)
}
}
}
return super.dispatchTouchEvent(event)
}
2. EditText 입력 포커스 및 해제 구분
// 배경 클릭으로 키보드 및 포커스 해제시 조건이 잘못됬을 경우 자동으로 재포커싱
private fun setReInputFocus(edit: EditText, message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
edit.postDelayed({
edit.requestFocus()
val imm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(edit, 0)
}, 500)
}
mNameEditText?.onFocusChangeListener = OnFocusChangeListener { view, hasFocus ->
if (hasFocus == true) {
// 포커스
} else {
// 해제
mNameEditText?.let {
if (it.text.length <= 1) {
setReInputFocus(it, "이름을 2자 이상 입력해주세요.")
}
}
}
}
3. EditText 글자 입력시 컨트롤 하기
mNameEditText?.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(charSequence: CharSequence?, start: Int, count: Int, after: Int) {
// 현재 입력한 글자 입력되 전 Text
}
override fun onTextChanged(charSequence: CharSequence?, start: Int, before: Int, count: Int) {
TODO("Not yet implemented")
}
override fun afterTextChanged(editable: Editable?) {
TODO("Not yet implemented")
}
})'Android(Kotlin)' 카테고리의 다른 글
| [Kotlin] 휴대폰 인증번호 자동추출 (0) | 2025.07.01 |
|---|---|
| [Kotlin]windowManager.defaultDisplay.getRealMetrics DEPRECATION 대응 코드 (0) | 2024.12.18 |
| [Kotlin] windowManager.defaultDisplay.getSize DEPRECATION 대응 (1) | 2024.12.18 |
| [Kotlin] AsyncTask Deplecated Observable.fromCallable() 로 대체하기 (0) | 2024.12.02 |
| [Kotlin] RxJava / retrofit2 이용한 API 서버 연동 (0) | 2024.11.28 |