package com.yidian.common.utils

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.ImageView
import com.yidian.common.extensions.hide
import com.yidian.common.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("")
    }
  }
}
