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); } } } }