dependency{
implementation 'gun0912.ted:tedpermission:2.2.3'
}
val mImm : InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
//EditText 화면 바깥 선택 시 키보드 숨기기
layout_login_container.setOnClickListener {
mImm.hideSoftInputFromWindow(edt_login_id.windowToken, 0)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
...
TedKeyboardObserver(this)
.listen { isShow ->
if (!isShow) {
// do checking EditText
edt_login_id.clearFocus()
edt_login_password.clearFocus()
}
}
...
}
class ClearEditText :
AppCompatEditText,
TextWatcher,
View.OnTouchListener,
View.OnFocusChangeListener {
private var clearDrawable: Drawable? = null
private var onFocus: OnFocusChangeListener? = null
private var onTouch: OnTouchListener? = null
constructor(context: Context?) : super(context!!)
constructor(context: Context?, attrs: AttributeSet) : super(context!!, attrs)
constructor(context: Context?, attrs: AttributeSet, defStyleAttr: Int)
: super(context!!, attrs, defStyleAttr)
init {
val temDrawable = ContextCompat.getDrawable(context, R.drawable.ic_expand_close)
clearDrawable = temDrawable?.let { DrawableCompat.wrap(it) }
clearDrawable?.let { DrawableCompat.setTintList(it, hintTextColors) }
clearDrawable?.setBounds(0,
0,
clearDrawable!!.intrinsicWidth,
clearDrawable!!.intrinsicHeight
)
setClearIconVisible(false)
super.setOnTouchListener(this)
super.setOnFocusChangeListener(this)
addTextChangedListener(this)
}
override fun onFocusChange(view: View?, hasFocus: Boolean) {
if(hasFocus){
setClearIconVisible(text?.length!! > 0)
}else{
setClearIconVisible(false)
}
if(onFocus != null){
onFocus?.onFocusChange(view, hasFocus)
}
}
override fun onTouch(view: View?, motionEvent: MotionEvent?): Boolean {
val x = motionEvent?.x?.toInt()
if(clearDrawable!!.isVisible && x!! > width - paddingRight - clearDrawable!!.intrinsicWidth){
if(motionEvent.action == MotionEvent.ACTION_UP){
text = null
}
return true
}
if (onTouch != null){
return onTouch!!.onTouch(view,motionEvent)
}else{
return false
}
}
override fun onTextChanged(
text: CharSequence?,
start: Int,
lengthBefore: Int,
lengthAfter: Int
) {
if(isFocused){
setClearIconVisible(text!!.isNotEmpty())
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun afterTextChanged(p0: Editable?) {}
private fun setClearIconVisible(visible : Boolean){
clearDrawable?.setVisible(visible,false)
setCompoundDrawables(null, null, if (visible) clearDrawable else null, null)
}
}
<com.sopt.ounce.util.ClearEditText
android:width="wrap_content"
android:height="wrap_content
...
/>