ClearableEdittext for xamarin android

2019-02-24 23:59发布

问题:

I have visited this link and many other links on the stack but I'm unable to find a similar solution for xamarin android : https://stackoverflow.com/a/14470930/7462031

I have implemented the frame layout solution but I want this solution throughout my application So the Clearable Edittext Looked interesting but the droid parts library is not available for xamarin so I am wondering if anyone has a solution for this problem in case you do kindly Help.!

回答1:

I achieved it by doing the following it might not be the best solution but I guess it is the only solution for Xamarin.Android available for this:

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

To change the x icon as your custom one change the image in init()