适用于 xamarin android 的 ClearableEdittext [英] ClearableEdittext for xamarin android

查看:36
本文介绍了适用于 xamarin android 的 ClearableEdittext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经访问了这个链接和堆栈上的许多其他链接,但我无法为 xamarin android 找到类似的解决方案:https://stackoverflow.com/a/14470930/7462031

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

我已经实现了框架布局解决方案,但我希望在我的整个应用程序中使用这个解决方案 所以可清除的 Edittext 看起来很有趣,但 droid 部件库不适用于 xamarin 所以我想知道是否有人可以解决这个问题,以防万一请帮助.

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.!

推荐答案

我通过执行以下操作实现了它它可能不是最好的解决方案,但我想它是 Xamarin.Android 的唯一可用解决方案:

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

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

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

这篇关于适用于 xamarin android 的 ClearableEdittext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆