xamarin-forms-docs/knowledge-base/maskedinput-set-virtual-key...

2.4 KiB

title description type page_title slug position tags ticketid res_type
How to implement keyboard support in RadMaskedInput How to set the virtual keyboard to the Xamarin MaskedInput control how-to Set numeric keyboard to MaskedInput through renderers on Android and iOS maskedinput-set-virtual-keyboard maskedinput, keyboard 1497050 kb

Environment

Product Version R3 2020
Product MaskedInput for Xamarin.Forms

Description

Currently RadMaskedInput does not provide Keyboard property, so users can define the virtual keyboard type. Still, this can be implemented through custom renderers on Android and iOS. The sample renderers below show how to define numeric virtual keyboard for the MaskedInput control.

Solution

  • Here is the solution on Android (replace "XamarinSampleApp" with the namespace used in your app):
using Android.Content;
using Telerik.XamarinForms.Input;
using Telerik.XamarinForms.InputRenderer.Android;
using Xamarin.Forms.Platform.Android;
using XamarinSampleApp.Droid;

[assembly: Xamarin.Forms.ExportRenderer(typeof(MaskedInputElement), typeof(MyCustomMaskRenderer))]
namespace XamarinSampleApp.Droid
{
    public class MyCustomMaskRenderer : MaskedInputRenderer
    {
        public MyCustomMaskRenderer(Context context) : base(context)
        { 
        }
        protected override void OnElementChanged(ElementChangedEventArgs<MaskedInputElement> e)
        {
            base.OnElementChanged(e);
            var inputType = this.Control.InputType;
            this.Control.SetRawInputType(Android.Text.InputTypes.ClassNumber);
        }
    }
}
  • And the custom renderer on iOS (replace "XamarinSampleApp" with the namespace used in your app):
using UIKit;
using Telerik.XamarinForms.Input;
using Telerik.XamarinForms.InputRenderer.iOS;
using Xamarin.Forms.Platform.iOS;
using XamarinSampleApp.iOS;

[assembly: Xamarin.Forms.ExportRenderer(typeof(MaskedInputElement), typeof(MyCustomMaskRenderer))]
namespace XamarinSampleApp.iOS
{
    public class MyCustomMaskRenderer : MaskedInputRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<MaskedInputElement> e)
        {
            base.OnElementChanged(e);
            this.Control.KeyboardType = UIKeyboardType.DecimalPad;
        }
    }
}