a
554325746@qq.com
2020-01-13 7171bbcdb2859ea93f3af69d817243752b08314a
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.mcivicm.editablespinner;
 
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.TextView;
 
import java.util.Arrays;
 
/**
 * 可编辑的下拉框
 */
 
public class EditableSpinner extends LinearLayout {
 
    private ImageView mPullDown;
    private EditText mTextView;
    private AdapterView.OnItemClickListener onItemClickListener;
    private TextWatcher textWatcher;
    private ListPopupWindow mPopup;
    private EditableSpinnerAdapter adapter;
    private boolean isIconUp = false;
 
    public EditableSpinner(Context context) {
        this(context, null, 0);
    }
 
    public EditableSpinner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public EditableSpinner(Context context, AttributeSet attrs, int style) {
        super(context, attrs, style);
        init(attrs, style);
    }
 
    private void init(AttributeSet attrs, int style) {
        View v = LayoutInflater.from(getContext()).inflate(R.layout.view_editable_spinner, this, false);
        addView(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPullDown = (ImageView) v.findViewById(R.id.pull_down);
        mTextView = (EditText) v.findViewById(R.id.content_view);
        mTextView.addTextChangedListener(new TextChangedListener());
        mPopup = new ListPopupWindow(getContext());
        mPopup.setModal(true);
        mPopup.setDropDownGravity(Gravity.BOTTOM);
        mPopup.setAnchorView(mTextView);
        mPopup.setOnDismissListener(new DismissListener());
        mPopup.setOnItemClickListener(new ItemClickListener());
        mPullDown.setOnClickListener(new PullDownClickListener());
    }
 
    /**
     * 设置下拉框的点击事件
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
 
    /**
     * @return 视图适配器
     */
    public EditableSpinnerAdapter getAdapter() {
        return adapter;
    }
 
    /**
     * @param adapter 设置视图适配器
     */
    public void setAdapter(EditableSpinnerAdapter adapter) {
        this.adapter = adapter;
    }
 
    /**
     * @return 输入框文字
     */
    public Editable getText() {
        return mTextView.getText();
    }
 
    /**
     * @param text 设置输入框文字
     */
    public void setText(String text) {
        mTextView.setText(text);
    }
 
    /**
     * 设置下拉字符串
     *
     * @param strings
     */
    public void setDropDownStrings(String... strings) {
        adapter = new DefaultEditableSpinnerAdapter(getContext(), Arrays.asList(strings));
        mPopup.setAdapter(adapter);
    }
 
    /**
     * 设置提示文本
     *
     * @param string
     */
    public void setHint(String string) {
        mTextView.setHint(string);
    }
 
    /**
     * 设置提示颜色
     *
     * @param color
     */
    public void setHintTextColor(int color) {
        mTextView.setHintTextColor(color);
    }
 
    //右侧下拉箭头点击时间
    private class PullDownClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            mTextView.requestFocus();
            if (isIconUp) {
                mPopup.dismiss();
            } else {
                hideSoftInput();//关闭输入法
                animateArrow(true);
                showPopup();
                isIconUp = true;
            }
        }
    }
 
    private void showPopup() {
        mPopup.setWidth(mTextView.getWidth());
        if (mPopup.getListView() != null) {
            mPopup.getListView().setVerticalScrollBarEnabled(false);
            mPopup.getListView().setHorizontalScrollBarEnabled(false);
        }
        mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        mPopup.show();
    }
 
    //旋转箭头
    private void animateArrow(boolean up) {
        float angle = up ? 180f : 0f;
        mPullDown.animate().rotation(angle).setDuration(100).start();
    }
 
    //下拉框点击事件
    private class ItemClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mTextView.setText(EditableSpinner.this.adapter.getDisplayString(position));
            mPopup.dismiss();
            if (EditableSpinner.this.onItemClickListener != null) {
                EditableSpinner.this.onItemClickListener.onItemClick(parent, view, position, id);
            }
        }
    }
 
    //下拉框消失监听
    private class DismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            animateArrow(false);
            isIconUp = false;
        }
    }
 
    //隐藏软件盘
    private void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindowToken(), 0);
    }
 
    /**
     * 设置文本颜色
     *
     * @param color
     */
    public void setTextColor(int color) {
        mTextView.setTextColor(color);
    }
 
    /**
     * 设置文本大小
     *
     * @param size
     */
    public void setTextSize(int size) {
        mTextView.setTextSize(size);
    }
 
    /**
     * 输入类型
     *
     * @param type
     */
    public void setInputType(int type) {
        mTextView.setInputType(type);
    }
 
    /**
     * 输入下拉框背景
     *
     * @param drawable
     */
    public void setPopupBackgroundDrawable(Drawable drawable) {
        mPopup.setBackgroundDrawable(drawable);
    }
 
    /**
     * 设置输入框的内边距
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setEditTextPadding(int left, int top, int right, int bottom) {
        mTextView.setPadding(left, top, right, bottom);
    }
 
    /**
     * 设置最大字符数
     *
     * @param maxLength
     */
    public void setEditTextMaxLength(int maxLength) {
        mTextView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
    }
 
    /**
     * 监听输入框输入完成
     */
    public void setOnAfterTextChanged(TextWatcher textWatcher){
        this.textWatcher = textWatcher;
    }
 
    private class TextChangedListener implements TextWatcher {
 
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if(EditableSpinner.this.textWatcher != null){
                EditableSpinner.this.textWatcher.beforeTextChanged(charSequence,i,i1,i2);
            }
        }
 
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if(EditableSpinner.this.textWatcher != null){
                EditableSpinner.this.textWatcher.onTextChanged(charSequence,i,i1,i2);
            }
        }
 
        @Override
        public void afterTextChanged(Editable editable) {
            if(EditableSpinner.this.textWatcher != null){
                EditableSpinner.this.textWatcher.afterTextChanged(editable);
            }
        }
    }
}