简体   繁体   English

屏蔽的Xamarin.forms入口控件

[英]Masked entry control Xamarin.forms

Masked entry control for Xamarin.forms. Xamarin.forms的屏蔽的入口控件。 I would like to have a entry field which will only allow to add values in format: xx:xx eg: 01:00, 25:98 etc. I tried setting keyboard property to numeric, but it doesn't help as it does not contain : 我想要一个输入字段,该字段仅允许添加以下格式的值:xx:xx,例如:01:00、25:98等。我尝试将键盘属性设置为数字,但是它没有帮助,因为它没有包含:

How can I do that. 我怎样才能做到这一点。 I am targeting all the platforms so should work for all. 我的目标是所有平台,因此应该适用于所有平台。

Do you want a special keyboard with only those controls or would an Entry that only showed those characters no matter what characters you typed work? 您是否想要只带有那些控件的特殊键盘,或者无论您键入什么字符都可以仅显示那些字符的Entry呢? If the latter would be ok, I would suggest attaching a behavior to your entry. 如果后者没问题,我建议您在行为中添加一个行为。

The below code will allow the user to type anything they want, but if what they type is not a number or a colon, then it will not show up in the entry, you could also display an error message of some kind if you wanted. 下面的代码将允许用户键入所需的任何内容,但是如果键入的内容不是数字或冒号,则该内容将不会显示在条目中,如果需要,您还可以显示某种错误消息。

/// <summary>
/// Will validate that the text entered into an Entry is a valid number string (allowing: numbers and colons).
/// </summary>
public class IntColonValidationBehavior : Behavior<Entry> {

    public static IntColonValidationBehavior Instance = new IntColonValidationBehavior();

    /// <summary>
    /// Attaches when the page is first created.
    /// </summary>
    protected override void OnAttachedTo(Entry entry) {
        entry.TextChanged += OnEntryTextChanged;
        base.OnAttachedTo(entry);
    }

    /// <summary>
    /// Detaches when the page is destroyed.
    /// </summary>
    protected override void OnDetachingFrom(Entry entry) {
        entry.TextChanged -= OnEntryTextChanged;
        base.OnDetachingFrom(entry);
    }

    private void OnEntryTextChanged(object sender, TextChangedEventArgs args) {

        if(!string.IsNullOrWhiteSpace(args.NewTextValue)) {
            int  result;
            string valueWithoutColon = args.NewTextValue.Replace(":", string.Empty);
            bool isValid = int.TryParse(valueWithoutColon, out result);

            ((Entry)sender).Text = isValid ? args.NewTextValue : args.NewTextValue.Remove(args.NewTextValue.Length - 1);
        }
    }
}

Then your Entry will look like so: 然后,您的条目将如下所示:

<Entry Placeholder="Enter an int or a colon">
    <Entry.Behaviors>
        <local:IntColonValidationBehavior.Instance />
    </Entry.Behaviors>
</Entry>

-OR- -要么-

Entry entry = new Entry { Placeholder = "Enter an int or a colon" };
entry.Behaviors.Add (IntColonValidationBehavior.Instance);

*Edit: *编辑:

Maybe replace the string.IsNullOrEmpty if statement with this (have not actually tested but you could tweak this to make it work for you): 也许用以下代码替换string.IsNullOrEmpty if (尚未经过实际测试,但您可以对其进行调整以使其适用于您):

        if(!string.IsNullOrWhiteSpace(args.NewTextValue)) {
            int  result;
            string[] splitValue = args.NewTextValue.Split(new [] { ":" }, StringSplitOptions.RemoveEmptyEntries);

            foreach(string value in splitValue) {
                if(value.Length > 2) {
                    ((Entry)sender).Text = args.NewTextValue.Remove(args.NewTextValue.Length - 1);
                    return;
                }

                bool isValid = int.TryParse(args.NewTextValue, out result);

                if(!isValid) {
                    ((Entry)sender).Text = args.NewTextValue.Remove(args.NewTextValue.Length - 1);
                    return;
                }
            }


            ((Entry)sender).Text = args.NewTextValue;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM