【问题标题】:Masked entry control Xamarin.forms蒙面的入口控件 Xamarin.forms
【发布时间】:2016-08-29 12:57:15
【问题描述】:

Xamarin.forms 的屏蔽入口控件。我想要一个只允许添加格式的值的输入字段:xx:xx 例如:01:00、25:98 等。我尝试将键盘属性设置为数字,但它没有帮助,因为它没有包含:

我该怎么做。我的目标是所有平台,所以应该适用于所有人。

【问题讨论】:

标签: c# xamarin.forms


【解决方案1】:

您想要一个只有这些控件的特殊键盘,还是想要一个无论您输入什么字符都只显示这些字符的条目?如果后者可以,我建议在您的条目中附加一个行为。

下面的代码将允许用户输入他们想要的任何内容,但如果他们输入的不是数字或冒号,则它不会显示在条目中,如果出现以下情况,您也可以显示某种错误消息你想要的。

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

那么您的条目将如下所示:

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

-或-

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

*编辑:

也许用这个替换 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;
        }

【讨论】:

  • 我用的是C#,不熟悉Xaml
  • 感谢您的快速响应,但我希望允许用户输入如下内容:xx:xx:xx where xx->number
  • @Arti 所以你需要它只允许他们输入 2 个数字,然后是一个冒号,然后是另外 2 个数字,等等?
  • 是的,我想要那个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 2021-04-20
相关资源
最近更新 更多