【问题标题】:How do I set the keyboard as a numeric keypad and can I enter a negative number in Xamarin.Forms?如何将键盘设置为数字小键盘,我可以在 Xamarin.Forms 中输入负数吗?
【发布时间】:2017-10-17 03:46:44
【问题描述】:

需求是显示键盘的默认数字可以这样输入负数:

但是没有办法为这种方式设置默认值。

如果设置了这个键盘,就不能输入负数,点击减号没有反应。

纯数字小键盘,但不能输入负数

有什么解决办法吗?

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.android


    【解决方案1】:

    您必须为 NumericEntry 创建自己的控件,例如:

    public class NumericTextBox : Entry
    {
        public NumericTextBox()
        {
            this.Keyboard = Keyboard.Numeric;
        }
    }
    

    然后,在您的 Android 项目中,创建自定义渲染器:

    [assembly: ExportRenderer(typeof(NumericTextBox), typeof(CustomNumericTextboxRenderer))]
    
    namespace xxx.Droid.Renderers
    {
        public class CustomNumericTextboxRenderer : EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
    
                var native = Control as EditText;
    
                native.InputType = Android.Text.InputTypes.ClassNumber | Android.Text.InputTypes.NumberFlagSigned | Android.Text.InputTypes.NumberFlagDecimal;
            }
        }
    }
    

    这已经过测试并适用于 Droid 设备。它允许输入带小数点的负/正值。它正确地禁止输入错误的十进制值。

    【讨论】:

    • @Jackson,您可以在 Wilson Vargas 的示例中将 TextBox 替换为 Entry
    • @York Shen - MSFT 非常感谢!
    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 2015-01-02
    • 2016-09-14
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2011-11-16
    相关资源
    最近更新 更多