how can I create clean Button in AutoCompleteTextView

后端 未结 3 660
轻奢々
轻奢々 2021-01-14 01:32

How can I create clean Button in AutoCompleteTextView, when I click clear button I want to clear all text in AutoCompleteTextView, see in picture

3条回答
  •  有刺的猬
    2021-01-14 02:12

    As explained in this post by Michael Derazon, you can extend your AutoCompleteTextView to include a custom clear function. The implementation will look like this:

    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.AutoCompleteTextView;
    
    /**
     * sub class of {@link android.widget.AutoCompleteTextView} that includes a clear (dismiss / close) button with
     * a OnClearListener to handle the event of clicking the button
     * based on code from {@link http://www.gubed.net/clearableautocompletetextview}
     * @author Michael Derazon
     *
     */
    public class ClearableAutoCompleteTextView extends AutoCompleteTextView {
        // was the text just cleared?
        boolean justCleared = false;
    
        // if not set otherwise, the default clear listener clears the text in the
        // text view
        private OnClearListener defaultClearListener = new OnClearListener() {
    
            @Override
            public void onClear() {
                ClearableAutoCompleteTextView et = ClearableAutoCompleteTextView.this;
                et.setText("");
            }
        };
    
        private OnClearListener onClearListener = defaultClearListener;
    
        // The image we defined for the clear button
        public Drawable imgClearButton = getResources().getDrawable(
                R.drawable.abc_ic_clear_holo_light);
    
        public interface OnClearListener {
            void onClear();
        }
    
        /* Required methods, not used in this implementation */
        public ClearableAutoCompleteTextView(Context context) {
            super(context);
            init();
        }
    
        /* Required methods, not used in this implementation */
        public ClearableAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        /* Required methods, not used in this implementation */
        public ClearableAutoCompleteTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        void init() {
            // Set the bounds of the button
            this.setCompoundDrawablesWithIntrinsicBounds(null, null,
                    imgClearButton, null);
    
            // if the clear button is pressed, fire up the handler. Otherwise do nothing
            this.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    
                    ClearableAutoCompleteTextView et = ClearableAutoCompleteTextView.this;
    
                    if (et.getCompoundDrawables()[2] == null)
                        return false;
    
                    if (event.getAction() != MotionEvent.ACTION_UP)
                        return false;
    
                    if (event.getX() > et.getWidth() - et.getPaddingRight() - imgClearButton.getIntrinsicWidth()) {
                        onClearListener.onClear();
                        justCleared = true;
                    }
                    return false;
                }
            });
        }
    
        public void setImgClearButton(Drawable imgClearButton) {
            this.imgClearButton = imgClearButton;
        }
    
        public void setOnClearListener(final OnClearListener clearListener) {
            this.onClearListener = clearListener;
        }
    
        public void hideClearButton() {
            this.setCompoundDrawables(null, null, null, null);
        }
    
        public void showClearButton() {
            this.setCompoundDrawablesWithIntrinsicBounds(null, null, imgClearButton, null);
        }
    
    }
    

    See the full post for a complete guide on how to implement it. The result will be as below picture:

提交回复
热议问题