如何使用UItextfield在xamarin IOS中只进行前进和后退文本框?

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

我想只在前向方向输入6位数的验证码,从左到右,在UITextfield上从右到左删除。使用不能集中在textfield之间..如果他想删除或更改任何文本框值,则必须从右侧删除并到达最左侧的文本框。你可以看到下面的图像

Verification code screen

xamarin xamarin.ios
1个回答
1
投票

首先,我们可以创建一个视图并在其中添加textView以进行编辑,如:

UIView containView = new UIView(new CGRect(0, 200, 300, 50));
View.AddSubview(containView);

UITextView textView = new UITextView();
textView.TintColor = UIColor.Clear;
textView.BackgroundColor = UIColor.Clear;
textView.TextColor = UIColor.Clear;
textView.Frame = containView.Bounds;
textView.Delegate = new MyTextViewDelegate(this);
textView.KeyboardType = UIKeyboardType.NumberPad;
containView.AddSubview(textView);

然后我们可以在其中绘制六个标签用于显示数字,六个指针线,也有底线,如:

for (int i = 0; i < 6; i++)
{
    UILabel label = new UILabel();
    label.Frame = new CGRect(i * 50, 0, 50, 50);
    label.TextAlignment = UITextAlignment.Center;
    label.TextColor = UIColor.Black;
    containView.AddSubview(label);
    labelArr.Add(label);

    UIView pointerLine = new UIView(new CGRect(i * 50 + 25, 12, 1, 26));
    pointerLine.BackgroundColor = UIColor.Blue;
    containView.AddSubview(pointerLine);
    pointerLine.Layer.AddAnimation(opacityAnimation(), "kOpacityAnimation");
    pointlineArr.Add(pointerLine);
    if (i > 0) pointerLine.Hidden = true;

    UIView line = new UIView(new CGRect(i * 50 + 5, 48, 40, 2));
    line.BackgroundColor = UIColor.Gray;
    containView.AddSubview(line);
    lineArr.Add(line);
}

为了在我们编辑时使pointerLine闪烁,我们可以向pointerLine添加一个动画,如:

CABasicAnimation opacityAnimation()
{
    CABasicAnimation opacityAnimation = CABasicAnimation.FromKeyPath("opacity");
    opacityAnimation.From = NSNumber.FromNFloat(1);
    opacityAnimation.To = NSNumber.FromNFloat(0);
    opacityAnimation.Duration = 0.9;
    opacityAnimation.RepeatCount = float.MaxValue;
    opacityAnimation.RemovedOnCompletion = true;
    return opacityAnimation;
}

最后,我们可以在textView的委托中进行一些配置,以便在我们尝试添加或删除数字时获得效果。这是我的委托文件供您参考:

public class MyTextViewDelegate : UITextViewDelegate
{
    ViewController superViewController;

    public MyTextViewDelegate(ViewController viewController)
    {
        superViewController = viewController;
    }

    public override void Changed(UITextView textView)
    {
        string verStr = textView.Text.Replace(" ", "");


        for (int i = 0; i < superViewController.labelArr.Count; i++)
        {
            UILabel label = superViewController.labelArr[i];
            UIView pointerLabel = superViewController.pointlineArr[i];

            if (i < verStr.Length)
            {
                changeViewLayer(i, true);
                label.Text = verStr.Substring(i, 1);

            }
            else
            {
                changeViewLayer(i, false);
                label.Text = "";
            }
            pointerLabel.Hidden = true;
        }

        if (verStr.Length >= 6)
        {
            textView.Text = verStr.Substring(0, 6);
            endEdit();
            return;
        } 
        else
        {
            superViewController.pointlineArr[verStr.Length].Hidden = false;
        }

    }

    void endEdit()
    {
        superViewController.View.EndEditing(true);
    }

    void changeViewLayer(int index, bool isHidden)
    {
        UIView line = superViewController.lineArr[index];

        if (isHidden)
        {
            line.BackgroundColor = UIColor.Red;
        } 
        else
        {
            line.BackgroundColor = UIColor.Gray;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.