hxb
2022-11-22 b3513b1713bb979d0a69c5a8c4ddcd038f184e6e
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.mm.android.deviceaddmodule.mobilecommon.widget;
 
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
 
/**
 * 通用搜索框
 */
 
public class CustomSearchView extends RelativeLayout implements View.OnClickListener,
        ClearEditText.IFocusChangeListener, ClearEditText.ITextChangeListener,
        TextView.OnEditorActionListener {
    private ClearEditText mSearchInput;
    private TextView mCancelTv;
    private InputMethodManager mInputMethodManager;
    private IOnTextChangeListener mTextChangeListener;
    private IOnFocusChangeListener mFocusChangeListener;
 
    public CustomSearchView(Context context) {
        this(context, null);
    }
 
    public CustomSearchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public interface IOnFocusChangeListener {
        void onSearchFocusChange(View v, boolean hasFocus);
    }
 
    public void setOnSearchFocusChangeListener(IOnFocusChangeListener listener) {
        mFocusChangeListener = listener;
    }
 
    public interface IOnTextChangeListener {
        void onTextChange(View v, CharSequence ch);
    }
 
    public void setOnTextChangedListener(IOnTextChangeListener listener) {
        mTextChangeListener = listener;
    }
 
    public CustomSearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
 
        LayoutInflater.from(context).inflate(R.layout.mobile_common_search_view, this);
        initView();
        initData();
    }
 
    private void initView() {
        mSearchInput =  findViewById(R.id.common_search_input);
        mCancelTv =  findViewById(R.id.common_search_cancel);
 
        mCancelTv.setOnClickListener(this);
        mSearchInput.setTextChangeListener(this);
        mSearchInput.setOnClickListener(this);
        mSearchInput.setClearTextFocusChange(this);
        mSearchInput.setOnEditorActionListener(this);
    }
 
    private void initData() {
        mInputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    }
 
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.common_search_cancel) {
            mSearchInput.clearFocus();
        } else if (id == R.id.common_search_input) {
            mSearchInput.requestFocus();
        }
    }
 
    @Override
    public void onClearTextFocusChange(View v, boolean hasFocus) {
        LogUtil.debugLog("33084", " onClearTextFocusChange hasFocus = " + hasFocus);
        if (hasFocus) {
            mCancelTv.setVisibility(View.VISIBLE);
        } else {
            mCancelTv.setVisibility(View.GONE);
            mSearchInput.setText("");
            mInputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
 
        if (mFocusChangeListener != null) {
            mFocusChangeListener.onSearchFocusChange(v, hasFocus);
        }
    }
 
    @Override
    public void afterChanged(EditText v, Editable s) {
 
    }
 
    @Override
    public void beforeChanged(EditText v, CharSequence s, int start, int count, int after) {
 
    }
 
    @Override
    public void onTextChanged(EditText v, CharSequence text, int start, int lengthBefore, int lengthAfter) {
        LogUtil.debugLog("33084", " onTextChanged");
        if (mTextChangeListener != null) {
            mTextChangeListener.onTextChange(v, text);
        }
    }
 
    @Override
    public boolean onEditorAction(TextView tv, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            if (mInputMethodManager != null) {
                mInputMethodManager.hideSoftInputFromWindow(tv.getWindowToken(), 0);
            }
            return true;
        }
        return false;
    }
 
    /**
     * 设置提示语
     *
     * @param resId
     */
    public void setSearchTextHint(int resId) {
        mSearchInput.setHint(getResources().getString(resId));
    }
 
    /**
     * 获取输入框内容
     */
    public String getEditText() {
        return mSearchInput.getText().toString();
    }
 
    private void onDestroyView() {
        if (mInputMethodManager != null) {
            mInputMethodManager = null;
        }
        if (mTextChangeListener != null) {
            mTextChangeListener = null;
        }
    }
}