package com.yidian.bcommon.utils

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.ImageView
import com.yidian.bcommon.extensions.hide
import com.yidian.bcommon.extensions.show

object EditTextUtils {

  /**
   * 输入框清空
   */
  fun clearEditText(editText: EditText, imageView: ImageView) {
    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?) {
        if (s?.length ?: 0 > 0) {
          imageView.show()
        } else {
          imageView.hide()
        }
      }
    })
    imageView.setOnClickListener {
      editText.setText("")
    }
  }
}
