我想要一个条目在占位符之前有一个图标,在条目末尾有一个图标,以显示和隐藏条目中的文本,我使用本教程有一个带有显示和隐藏图标的条目: https://www.techierathore.com/2017/09/xamarin-forms-tip-implement-show-hide-password-using-effects/
但现在我也想在条目之前有图标,我可以通过本教程做到这一点: https://xamgirl.com/image-entry-in-xamarin-forms/
但如果我将第一个教程的效果添加到自定义条目中,则只会显示隐藏/显示图标。
有可能做我想做的事吗?
最佳答案
你可以使用 editText.SetCompoundDrawablesRelativeWithIntrinsicBounds()
添加两个图标。
SetCompoundDrawablesRelativeWithIntrinsicBounds
接受四个参数,用于绘制开始、顶部、结束和底部。在第一个教程中,隐藏/显示图标添加到最后,您可以将第一个参数从 0 更改为您的 drawable。
需要修改三个地方。
例如:
public class ShowHidePassEffect : PlatformEffect
{
protected override void OnAttached()
{
ConfigureControl();
}
protected override void OnDetached()
{
}
private void ConfigureControl()
{
EditText editText = ((EditText)Control);
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.Password, 0, Resource.Drawable.ShowPass, 0);
editText.SetOnTouchListener(new OnDrawableTouchListener());
}
}
public class OnDrawableTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener
{
public bool OnTouch(Android.Views.View v, MotionEvent e)
{
if (v is EditText && e.Action == MotionEventActions.Up)
{
EditText editText = (EditText)v;
if (e.RawX >= (editText.Right - editText.GetCompoundDrawables()[2].Bounds.Width()))
{
if (editText.TransformationMethod == null)
{
editText.TransformationMethod = PasswordTransformationMethod.Instance;
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.Password, 0, Resource.Drawable.ShowPass, 0);
}
else
{
editText.TransformationMethod = null;
editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(Resource.Drawable.Password, 0, Resource.Drawable.HidePass, 0);
}
return true;
}
}
return false;
}
}
关于c# - 输入显示和隐藏密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50512908/