riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cn.flightfeather.thirdappmodule.view
 
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import cn.flightfeather.thirdappmodule.R
 
/**
 * @author riku
 * Date: 2020/12/22
 */
class SearchView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : FrameLayout(context, attrs, defStyleAttr) {
 
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
 
    constructor(context: Context) : this(context, null, 0)
 
    private lateinit var e: EditText
 
    private val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
 
    var onConfirm: (str: String) -> Unit = { _ -> }
 
    init {
        initView()
    }
 
    private fun initView() {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_search, this)
        e = view.findViewById(R.id.edt_content)
        val c = view.findViewById<ImageView>(R.id.img_clear)
        val y = view.findViewById<TextView>(R.id.txt_confirm)
        val n = view.findViewById<TextView>(R.id.txt_cancel)
        e.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
            override fun afterTextChanged(s: Editable?) {
                if (s.isNullOrBlank()) {
                    c.visibility = View.GONE
//                    y.visibility = View.GONE
//                    n.visibility = View.VISIBLE
                } else {
                    c.visibility = View.VISIBLE
//                    y.visibility = View.VISIBLE
//                    n.visibility = View.GONE
                }
 
            }
        })
        c.setOnClickListener {
            e.setText("")
        }
        y.setOnClickListener {
            this.visibility = View.GONE
            imm.hideSoftInputFromWindow(windowToken, 0)
            onConfirm(e.text.toString())
        }
        n.setOnClickListener {
            e.setText("")
            imm.hideSoftInputFromWindow(windowToken, 0)
            this.visibility = View.GONE
        }
    }
 
    fun show(hint: String = "", default: String = "") {
        this.visibility = View.VISIBLE
        e.hint = hint
        e.setText(default)
        e.requestFocus()
        imm.showSoftInput(e, InputMethodManager.SHOW_IMPLICIT);
    }
}