gpt4 book ai didi

android - xamarin android 的 ClearableEdittext

转载 作者:太空狗 更新时间:2023-10-29 13:07:20 24 4
gpt4 key购买 nike

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

我已经实现了框架布局解决方案,但我希望在我的整个应用程序中使用此解决方案所以可清除的 Edittext 看起来很有趣,但机器人零件库不适用于 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() 中的图像

关于android - xamarin android 的 ClearableEdittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46069323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com