Xamarin传统方法中显示/隐藏密码切换功能的最佳方法

问题描述 投票:0回答:1

我们正在使用Xamarin传统方法开发显示/隐藏密码切换功能。实施它的最佳地点是什么?是在Xamarin.iOS&中吗? Droid还是在Xamarin.Core中?

如果在Xamarin.Core中,请告诉我们该过程。是通过价值转换器吗?

提前感谢。

xamarin xamarin.android xamarin.ios mvvmcross
1个回答
0
投票

在使用效果输入密码时,我们总是使用自定义控件来显示/隐藏密码。

Android:

在'OnDrawableTouchListener'方法中手动创建控件,在该方法中,我们将ShowPass和HidePass图标添加到输入控件,根据用户的触摸操作对其进行更改,并将其附加到效果调用上,当添加效果时将触发该效果控件。

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(0, 0, Resource.Drawable.ShowPass, 0);  
                }  
                else  
                {  
                    editText.TransformationMethod = null;  
                    editText.SetCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, Resource.Drawable.HidePass, 0);  
                }  

                return true;  
            }  
        }  

        return false;  
    }  
}  

结果:

enter image description here

IOS:

在'ConfigureControl'方法中手动创建控件,在该方法中,我们将ShowPass和HidePassicons添加到条目控件中,并根据用户的触摸操作对其进行更改;并将其附加到效果调用上,当将效果添加到控件时将触发该效果。

private void ConfigureControl()  
    {  
        if (Control != null)  
        {  
            UITextField vUpdatedEntry = (UITextField)Control;  
            var buttonRect = UIButton.FromType(UIButtonType.Custom);  
            buttonRect.SetImage(new UIImage("ShowPass"), UIControlState.Normal);  
            buttonRect.TouchUpInside += (object sender, EventArgs e1) =>  
            {  
                if (vUpdatedEntry.SecureTextEntry)  
                {  
                    vUpdatedEntry.SecureTextEntry = false;  
                    buttonRect.SetImage(new UIImage("HidePass"), UIControlState.Normal);  
                }  
                else  
                {  
                    vUpdatedEntry.SecureTextEntry = true;  
                    buttonRect.SetImage(new UIImage("ShowPass"), UIControlState.Normal);  
                }  
            };  

            vUpdatedEntry.ShouldChangeCharacters += (textField, range, replacementString) =>  
            {  
                string text = vUpdatedEntry.Text;  
                var result = text.Substring(0, (int)range.Location) + replacementString + text.Substring((int)range.Location + (int)range.Length);  
                vUpdatedEntry.Text = result;  
                return false;  
            };  


            buttonRect.Frame = new CoreGraphics.CGRect(10.0f, 0.0f, 15.0f, 15.0f);  
            buttonRect.ContentMode = UIViewContentMode.Right;  

            UIView paddingViewRight = new UIView(new System.Drawing.RectangleF(5.0f, -5.0f, 30.0f, 18.0f));  
            paddingViewRight.Add(buttonRect);  
            paddingViewRight.ContentMode = UIViewContentMode.BottomRight;  


            vUpdatedEntry.LeftView = paddingViewRight;  
            vUpdatedEntry.LeftViewMode = UITextFieldViewMode.Always;  

            Control.Layer.CornerRadius = 4;  
            Control.Layer.BorderColor = new CoreGraphics.CGColor(255, 255, 255);  
            Control.Layer.MasksToBounds = true;  
            vUpdatedEntry.TextAlignment = UITextAlignment.Left;  
        }  

    }  

结果:

enter image description here

更多详细信息,请参阅下面的文章。https://www.c-sharpcorner.com/article/xamarin-forms-tip-implement-show-hide-password-using-effects/

您可以从GitHub下载源文件以供参考。https://github.com/techierathore/ShowHidePassEx.git

© www.soinside.com 2019 - 2024. All rights reserved.