xamarin表现行为:只允许条目中的字符串而不是数字

时间:2017-09-09 17:24:46

标签: c# xamarin.forms

我使用下面的代码禁止用户在接受数字的条目中键入字符串。如何为一个只接受字符串而不是数字的条目反转这个。

            private static void OnEntryTextChanged(object sender, TextChangedEventArgs args) {

                if(!string.IsNullOrWhiteSpace(args.NewTextValue)) { 
                    bool isValid = args.NewTextValue.ToCharArray().All(IsDigit); //Make sure all characters are numbers

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

            /// <summary>
            /// Check to see if a character is a digit.
            /// </summary>
            /// <param name="c">The character to check.</param>
            /// <returns><c>true</c> if the character is between <c>0</c> and <c>9</c>.</returns>
            private static bool IsDigit(char c) {
                if(c >= 48) {
                    return c <= 57;
                }

                return false;
            }

1 个答案:

答案 0 :(得分:0)

你应该使用正则表达式。这个正则表达式只允许使用字母:

public class NavigationPresenter implements Initializable {

    @FXML
    Button atNavButton;

    @Inject
    private ViewState viewState ;

    private ResourceBundle resources = null;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.resources = resources;
    }

    @FXML
    void showDfdScene(ActionEvent event) {
        viewState.setAtShowing(true);
    }  
}

现在你的方法应该这样写:

^[a-zA-Z]+$

我不知道您的const string numberRegex = @"^[a-zA-Z]+$"; private static void OnEntryTextChanged(object sender, TextChangedEventArgs args) { if(!string.IsNullOrWhiteSpace(args.NewTextValue)) { IsValid = (Regex.IsMatch(args.NewTextValue, numberRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))); ((Entry)sender).Text = IsValid ? args.NewTextValue : args.NewTextValue.Remove(args.NewTextValue.Length -1); } } 是否正确,但您可以像这样创建:

Behavior

在你的View(xaml文件)中这样:

public class EntryValidatorBehavior: Behavior<Entry>
{
    const string numberRegex = @"^[a-zA-Z]+$";

    static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(EmailValidatorBehavior), false);

    public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty;

    public bool IsValid
    {
        get { return (bool)base.GetValue(IsValidProperty); }
        private set { base.SetValue(IsValidPropertyKey, value); }
    }

    protected override void OnAttachedTo(Entry bindable)
    {
        bindable.TextChanged += HandleTextChanged;
    }

    void HandleTextChanged(object sender, TextChangedEventArgs e)
    {
        IsValid = (Regex.IsMatch(e.NewTextValue, numberRegex, RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250)));
        ((Entry)sender).Text = IsValid ? e.NewTextValue : e.NewTextValue.Remove(e.NewTextValue.Length -1);
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        bindable.TextChanged -= HandleTextChanged;
    }
}