输入显示和隐藏密码 [英] Entry show and hide password
问题描述
我希望一个条目在占位符前有一个图标,在该条目的末尾有一个图标,以显示和隐藏该条目中的文本,使用本教程,我有一个带有show and hide图标的条目: https://www .techierathore.com/2017/09/xamarin-forms-tip-implement-show-hide-password-using-effects/
I want an entry to have an icon before the placeholder, and a icon in the end of the entry, to show and hide the text in the entry, i had an entry with the show and hide icon using this tutorial: https://www.techierathore.com/2017/09/xamarin-forms-tip-implement-show-hide-password-using-effects/
但是现在我也想在条目之前也有图标,我可以通过本教程来做到这一点: https://xamgirl.com/image-entry-in-xamarin-forms/
But now I want to have icons before the entry too, I can do that with this tutorial: https://xamgirl.com/image-entry-in-xamarin-forms/
但是,如果我将第一个教程的效果添加到自定义条目中,则只会显示和隐藏/显示图标.
But if i add the effect of the first tutorial to the custom entry, only the and hide/show icon shows up.
可以做我想做的事吗?
推荐答案
You could use editText.SetCompoundDrawablesRelativeWithIntrinsicBounds()
to add both icon.
SetCompoundDrawablesRelativeWithIntrinsicBounds
带有四个可绘制开始,顶部,结束和底部参数.在第一个教程中,将隐藏/显示"图标添加到末尾,您可以将第一个参数从0更改为可绘制对象.
有三个地方需要修改.
SetCompoundDrawablesRelativeWithIntrinsicBounds
takes four parameters for start, top, end, and bottom drawable. In the first tutorial, the hide/show icon is added to the end, you can change the first parameter from 0 to your drawable.
There are three places need to modify.
例如:
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;
}
}
结果是:
And the result is:
这篇关于输入显示和隐藏密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!