Forked from rodrigohenriques/ClickToSelectEditText.java
Last active
October 11, 2017 19:44
-
-
Save javier-delgado/323d232de04785f3c310fbeb9d84c99c to your computer and use it in GitHub Desktop.
Alternativa a los spinners en android utilizando editText
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ClickToSelectEditText<T> extends android.support.v7.widget.AppCompatEditText { | |
| List<T> mItems; | |
| String[] mListableItems; | |
| CharSequence mHint; | |
| int mSelectedIndex = -1; | |
| OnItemSelectedListener<T> onItemSelectedListener; | |
| public ClickToSelectEditText(Context context) { | |
| super(context); | |
| init(); | |
| } | |
| public ClickToSelectEditText(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| init(); | |
| } | |
| public ClickToSelectEditText(Context context, AttributeSet attrs, int defStyleAttr) { | |
| super(context, attrs, defStyleAttr); | |
| init(); | |
| } | |
| private void init() { | |
| this.mHint = getHint(); | |
| } | |
| @Override | |
| protected void onDraw(Canvas canvas) { | |
| super.onDraw(canvas); | |
| setFocusable(false); | |
| setClickable(true); | |
| setCompoundDrawablesWithIntrinsicBounds(null, null, getCaret(), null); | |
| setPadding(getPaddingLeft(), getPaddingTop(), MeasureUtils.dipToPixel(8, getContext()), getPaddingBottom()); | |
| setCompoundDrawablePadding(MeasureUtils.dipToPixel(8, getContext())); | |
| } | |
| private Drawable getCaret() { | |
| int size = MeasureUtils.dipToPixel(10, getContext()); | |
| Drawable triangle = getResources().getDrawable(R.drawable.ic_arrow_down); | |
| Bitmap bitmap = ((BitmapDrawable) triangle).getBitmap(); | |
| triangle = DrawableCompat.wrap(new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, size, size, true))); | |
| DrawableCompat.setTint(triangle.mutate(), getResources().getColor(R.color.grey700)); | |
| return triangle; | |
| } | |
| public void setItems(@ArrayRes int arrayRes) { | |
| setItems(Arrays.asList(getContext().getResources().getStringArray(arrayRes))); | |
| } | |
| public void setItems(List items) { | |
| if (items.isEmpty()) return; | |
| this.mItems = items; | |
| this.mListableItems = new String[items.size()]; | |
| int i = 0; | |
| for (Object item : mItems) { | |
| mListableItems[i++] = item.toString(); | |
| } | |
| configureOnClickListener(); | |
| } | |
| private void configureOnClickListener() { | |
| setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); | |
| builder.setTitle(getContext().getString(R.string.choose)); | |
| builder.setItems(mListableItems, new DialogInterface.OnClickListener() { | |
| @Override | |
| public void onClick(DialogInterface dialogInterface, int selectedIndex) { | |
| setSelectedItem(selectedIndex); | |
| if (onItemSelectedListener != null) { | |
| onItemSelectedListener.onItemSelectedListener(mItems.get(selectedIndex), selectedIndex); | |
| } | |
| } | |
| }); | |
| builder.create().show(); | |
| } | |
| }); | |
| } | |
| public void setOnItemSelectedListener(OnItemSelectedListener<T> onItemSelectedListener) { | |
| this.onItemSelectedListener = onItemSelectedListener; | |
| } | |
| public void setSelectedItem(T item) { | |
| setSelectedItem(mItems.indexOf(item)); | |
| } | |
| public void setSelectedItem(int idx) { | |
| setText(mListableItems[idx]); | |
| mSelectedIndex = idx; | |
| } | |
| public interface OnItemSelectedListener<T> { | |
| void onItemSelectedListener(T item, int selectedIndex); | |
| } | |
| public @Nullable | |
| T getSelectedItem() { | |
| if (mSelectedIndex < 0 || mSelectedIndex >= mItems.size()) | |
| return null; | |
| else | |
| return mItems.get(mSelectedIndex); | |
| } | |
| /** | |
| * Esto impide que se muestre el dialog de copiar y pegar | |
| * @return | |
| */ | |
| @Override | |
| public int getSelectionStart() { | |
| for (StackTraceElement element : Thread.currentThread().getStackTrace()) { | |
| if (element.getMethodName().equals("canPaste")) { | |
| return -1; | |
| } | |
| } | |
| return super.getSelectionStart(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface Listable { | |
| String getLabel(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @BindView(R.id.signup_text_input_job_category) | |
| ClickToSelectEditText<JobCategory> editTextJobCategory; | |
| editTextJobCategory.setItems(jobCategories); | |
| editTextJobCategory.setOnItemSelectedListener(new ClickToSelectEditText.OnItemSelectedListener<JobCategory>() { | |
| @Override | |
| public void onItemSelectedListener(JobCategory item, int selectedIndex) { | |
| selectedJobCategory = item; | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <android.support.design.widget.TextInputLayout | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content"> | |
| <com.your.package.ClickToSelectEditText | |
| android:id="@+id/signup_text_input_job_category" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:hint="@string/hint_job_category" /> | |
| </android.support.design.widget.TextInputLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
