Вход показать и скрыть пароль - PullRequest
0 голосов
/ 24 мая 2018

Я хочу, чтобы запись имела значок перед заполнителем и значок в конце записи, чтобы показать и скрыть текст в записи. У меня была запись со значком шоу и скрытия с использованием этого урока: https://www.techierathore.com/2017/09/xamarin-forms-tip-implement-show-hide-password-using-effects/

Но теперь я хочу, чтобы значки тоже были перед записью, я могу сделать это с помощью этого урока: https://xamgirl.com/image-entry-in-xamarin-forms/

Но если я добавлю эффект первого урока в пользовательскийвведите только значок и скрыть / показать.

Можно ли делать то, что я хочу?

Ответы [ 2 ]

0 голосов
/ 29 мая 2018

Вы можете использовать editText.SetCompoundDrawablesRelativeWithIntrinsicBounds() для добавления обоих значков.

SetCompoundDrawablesRelativeWithIntrinsicBounds принимает четыре параметра: начало, верх, конец и нижний элемент рисования.В первом уроке иконка скрытия / показа добавляется в конец, вы можете изменить первый параметр с 0 на ваш нарисованный.Есть три места, которые нужно изменить.

Например:

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;
    }
}

И результат:enter image description here

0 голосов
/ 24 мая 2018

Вы можете использовать что-то вроде этого (решение для Xamarin iOS):

txt.TextWithIcon(myIcon, Colors.Black, Colors.Blue, UIReturnKeyType.Next);

Кто вызывает этот метод

public static UITextField TextWithIcon(this UITextField text,
            string icon, UIColor colorBorder, UIColor colorText,
            UIReturnKeyType returnkey)
{
    text.LeftView = null; 
    var myImg = Images.LoadImageView(icon, UIViewContentMode.Center);
    myImg.Frame = new CGRect(10, 7, myImg.Frame.Width, myImg.Frame.Height);
    myImg.SizeToFit();

    var view = new UIView(new CGRect(0, 0, widthScreen, 70));
    view.AddSubview(myImg);

    text.LeftView = view;
    text.LeftViewMode = UITextFieldViewMode.Always;
    text.colorText = textColor;
    text.Layer.BorderWidth = 1f;
    text.Layer.BorderColor = colorBorder.CGColor;

    text.AttributedPlaceholder = new Foundation.NSAttributedString("placeholder", null, Colors.Black);
    text.ReturnKeyType = returnkey;
    text.SecureTextEntry = false;
    return text;
}

Надеюсь, это поможет

...