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
package com.mcivicm.editablespinner;
 
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.Gravity;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
 
import com.rengwuxian.materialedittext.MaterialEditText;
 
import java.util.Arrays;
 
/**
 * 材料视角可编辑的下拉框
 */
 
public class MaterialEditableSpinner extends LinearLayout {
    private ImageView mPullDown;
    private MaterialEditText mTextView;
    private AdapterView.OnItemClickListener onItemClickListener;
    private ListPopupWindow mPopup;
    private EditableSpinnerAdapter adapter;
    private boolean isIconUp = false;
 
    public MaterialEditableSpinner(Context context) {
        this(context, null, 0);
    }
 
    public MaterialEditableSpinner(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public MaterialEditableSpinner(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_material_editable_spinner, this, false);
        addView(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPullDown = (ImageView) v.findViewById(R.id.pull_down);
        mTextView = (MaterialEditText) v.findViewById(R.id.content_view);
        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());
    }
 
    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
 
    public Editable getText() {
        return mTextView.getText();
    }
 
    public void setText(String text) {
        mTextView.setText(text);
    }
 
    public void setStrings(String... strings) {
        adapter = new DefaultEditableSpinnerAdapter(getContext(), Arrays.asList(strings));
        mPopup.setAdapter(adapter);
    }
 
    public EditableSpinnerAdapter getAdapter() {
        return adapter;
    }
 
    public void setAdapter(EditableSpinnerAdapter adapter) {
        this.adapter = adapter;
    }
 
    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_ADJUST_RESIZE);
        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(MaterialEditableSpinner.this.adapter.getDisplayString(position));
            mPopup.dismiss();
            if (MaterialEditableSpinner.this.onItemClickListener != null) {
                MaterialEditableSpinner.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);
    }
 
    public void setFloatingLabel(int mode) {
        mTextView.setFloatingLabel(mode);
    }
 
    public void setFloatingLabelText(String text) {
        mTextView.setFloatingLabelText(text);
    }
 
    /**
     * 设置提示文本
     *
     * @param string
     */
    public void setHint(String string) {
        mTextView.setHint(string);
    }
 
    /**
     * 设置提示颜色
     *
     * @param color
     */
    public void setHintTextColor(int color) {
        mTextView.setHintTextColor(color);
    }
 
    /**
     * 设置文本颜色
     *
     * @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.setPaddings(left, top, right, bottom);
    }
 
    /**
     * 设置最大字符数
     *
     * @param maxLength
     */
    public void setEditTextMaxLength(int maxLength) {
        mTextView.setMaxCharacters(maxLength);
    }
 
}