代码之家  ›  专栏  ›  技术社区  ›  FreakyAli

xamarin android的ClearableEdittext

  •  3
  • FreakyAli  · 技术社区  · 8 年前

    https://stackoverflow.com/a/14470930/7462031

    1 回复  |  直到 7 年前
        1
  •  2
  •   FreakyAli    8 年前

    我是通过以下步骤实现的,这可能不是最好的解决方案,但我想这是Xamarin的唯一解决方案。Android可用于:

     public class ClearableEditext : EditText
    {
        Context mContext;
        Drawable imgX;
    
        public ClearableEditext(Context context) : base(context)
        {
            init(context, null);
        }
        public ClearableEditext(Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
        {
            init(context, attrs);
        }
        public ClearableEditext(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
        {
            init(context, attrs);
        }
        public ClearableEditext(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
        {
            init(context, attrs);
        }
    
        public void init(Context ctx, Android.Util.IAttributeSet attrs)
        {
            mContext = ctx;
            imgX = ContextCompat.GetDrawable(ctx, Android.Resource.Drawable.PresenceOffline);
            imgX.SetBounds(0, 0, imgX.IntrinsicWidth, imgX.IntrinsicHeight);
            manageClearButton();
            this.SetOnTouchListener(new TouchHelper(this, imgX));
            this.AddTextChangedListener(new TextListener(this));
        }
    
        public void manageClearButton()
        {
            if (this.Text.ToString().Equals(""))
                removeClearButton();
            else
                addClearButton();
        }
        public void addClearButton()
        {
            this.SetCompoundDrawables(this.GetCompoundDrawables()[0],
                    this.GetCompoundDrawables()[1],
                    imgX,
                    this.GetCompoundDrawables()[3]);
        }
        public void removeClearButton()
        {
            this.SetCompoundDrawables(this.GetCompoundDrawables()[0],
                    this.GetCompoundDrawables()[1],
                    null,
                    this.GetCompoundDrawables()[3]);
        }
    }
    
    public class TouchHelper : Java.Lang.Object, View.IOnTouchListener
    {
        ClearableEditext Editext;
        public ClearableEditext objClearable { get; set; }
        Drawable imgX;
        public TouchHelper(ClearableEditext editext, Drawable imgx)
        {
            Editext = editext;
            objClearable = objClearable;
            imgX = imgx;
        }
        public bool OnTouch(View v, MotionEvent e)
        {
            ClearableEditext et = Editext;
    
            if (et.GetCompoundDrawables()[2] == null)
                return false;
            // Only do this for up touches
            if (e.Action != MotionEventActions.Up)
                return false;
            // Is touch on our clear button?
            if (e.GetX() > et.Width - et.PaddingRight - imgX.IntrinsicWidth)
            {
                Editext.Text = string.Empty;
                if (objClearable != null)
                    objClearable.removeClearButton();
    
            }
            return false;
        }
    }
    
    public class TextListener : Java.Lang.Object, ITextWatcher
    {
        public ClearableEditext objClearable { get; set; }
        public TextListener(ClearableEditext objRef)
        {
            objClearable = objRef;
        }
    
        public void AfterTextChanged(IEditable s)
        {
    
        }
    
        public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
        {
    
        }
    
        public void OnTextChanged(ICharSequence s, int start, int before, int count)
        {
            if (objClearable != null)
                objClearable.manageClearButton();
        }
    }
    

    要将x图标更改为自定义图标,请更改init()中的图像