限制输入的长度和字符输入字段中Xamarin

限制输入的长度和字符输入字段中Xamarin

本文介绍了限制输入的长度和字符输入字段中Xamarin.Forms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能限制在Xamarin.Forms条目控件中输入长度和字符。我需要创建一个自定义的控制?有没有一种方法可以让我从条目(或其他控件)得出这样我就可以应用必要的每个平台的输入限制。

How can I restrict the length and characters entered in an Entry control in Xamarin.Forms. Do I need to create a custom control? Is there a way I can derive from Entry (or another control) so I can apply the necessary per-platform input limitations.

一个例子是数值型字段是限制为最多3个字符,数字而已。

An example would be a numeric field that is restricted to a maximum of 3 characters, digits only.

设置条目控制Keyboard.Numeric的键盘属性只设置键盘适用于iOS。它不会限制实际的文本输入 - 即我仍然可以进入非数字字符。我也不明白的方式来限制进入的长度。

Setting the Keyboard property of an Entry control to Keyboard.Numeric only sets the keyboard for iOS. It does not restrict the actual text entry - i.e. I can still enter non-digit characters. Nor do I see a way to limit the length of entry.

推荐答案

您可以限制在输入字段作为charecters数下面给出

You can restrict the number of charecters in the Entry field as given below,

  int restrictCount = <your restriction length> //Enter your number of character restriction
  Entry entry = new Entry();
  entry.TextChanged += OnTextChanged;

  void OnTextChanged(object sender, EventArgs e)
  {
    Entry entry = sender as Entry;
    String val = entry.Text; //Get Current Text

    if(val.Length > restrictCount)//If it is more than your character restriction
    {
     val = val.Remove(val.Length - 1);// Remove Last character
     entry.Text = val; //Set the Old value
    }
  }

这篇关于限制输入的长度和字符输入字段中Xamarin.Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:21