【问题标题】:ClearableEdittext for xamarin android适用于 xamarin android 的 ClearableEdittext
【发布时间】:2017-09-06 07:32:25
【问题描述】:

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

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

【问题讨论】:

  • 您看到的 'drioidparts' 库是您在 github github.com/droidparts/droidparts/tree/master/droidparts-misc 上找到的整个项目的一部分,您可以将此项目转换为 Xamarin
  • @AaronThompson 我实际上尝试将代码转换为 xamarin,但它无法正常工作我无法找到我什至用谷歌搜索的某些东西,但我找不到有用的东西你能帮忙一点吗多一点我有几个问题,如果你能回答它们,那将是一个巨大的帮助。谢谢
  • 我认为最好的选择是使用@Jaydeep Khamar 建议的最佳答案,它可能不是最优雅的解决方案,但您不需要将整个 java 项目转换为 c# 即可工作
  • 是的,我想无论如何我都必须这样做,感谢您的帮助! @亚伦汤普森

标签: android xamarin xamarin.forms xamarin.android android-widget


【解决方案1】:

我通过执行以下操作实现了它,它可能不是最好的解决方案,但我想它是 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() 中的图像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多