Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xamarin android的ClearableEdittext_Android_Xamarin_Xamarin.forms_Xamarin.android_Android Widget - Fatal编程技术网

xamarin android的ClearableEdittext

xamarin android的ClearableEdittext,android,xamarin,xamarin.forms,xamarin.android,android-widget,Android,Xamarin,Xamarin.forms,Xamarin.android,Android Widget,我已经访问了此链接和堆栈上的许多其他链接,但我无法为xamarin android找到类似的解决方案: 我已经实现了框架布局解决方案,但我希望在我的整个应用程序中都使用此解决方案,因此可清除的Edittext看起来很有趣,但droid零件库不适用于xamarin,因此我想知道是否有人可以解决此问题,以防您提供帮助 我通过以下方式实现了这一点,这可能不是最好的解决方案,但我想这是Xamarin的唯一解决方案。Android可用于此: public class ClearableEditext

我已经访问了此链接和堆栈上的许多其他链接,但我无法为xamarin android找到类似的解决方案:


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

我通过以下方式实现了这一点,这可能不是最好的解决方案,但我想这是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()中的图像

您看到的“drioidparts”库是您在github上找到的整个项目的一部分,您可以将该项目转换为Xamarin@AaronThompson实际上我试着将代码转换成xamarin,但它不能正常工作我找不到一些我甚至在谷歌上搜索过的东西,但我找不到有用的东西你能帮忙吗还有一点,我有几个问题,如果你能回答的话,会有很大的帮助。谢谢,我认为最好的选择是使用@Jaydeep Khamar建议的最佳答案。这可能不是最优雅的解决方案,但您不需要将整个java项目转换为c#,这样它就可以工作了。是的,我想我必须这样做,谢谢您的帮助@阿伦汤普森