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