Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

To dismiss the keyboard upon entering the last OTP digit

InfinityInfinity Member ✭✭
edited August 5 in Xamarin.Forms

Hello Guys,

I am working on a concept which has a OTP screen.
The requirement is to dismiss the keyboard automatically upon entering the last digit of the 6 digit OTP number.

Here is what I have done till now -

if (lastOTPInputEntry.Value != string.Empty)
{
    lastOTPInputEntry.Unfocus();
}

The I have an EntryRenderer which overrides this method -

protected override void OnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e). 
{ 
   if (Control != null). 
   {    
        if (e.Focus)
        {
             Control.RequestFocus();
        }
        else
        {
            Control.ClearFocus();
        }
    }
}

Control is a FormsEditText
But somehow the keyboard does not dismiss.
What am I doing wrong here .. ?

Answers

  • InfinityInfinity Member ✭✭

    Can anyone please help out here ..

  • jezhjezh Member, Xamarin Team Xamurai

    You can try to use DependencyService to achieve this.

    1.define an interface IKeyboardHelper

      public interface IKeyboardHelper
    {
        void HideKeyboard();
    }
    

    2.in Droid:

      [assembly: Xamarin.Forms.Dependency(typeof(DroidKeyboardHelper))]
    namespace FormApp.Droid
    {
        public class DroidKeyboardHelper : IKeyboardHelper
        {
            public void HideKeyboard()
            {
                var context = Forms.Context;
                var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
                if (inputMethodManager != null && context is Activity)
                {
                    var activity = context as Activity;
                    var token = activity.CurrentFocus?.WindowToken;
                    inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
    
                    activity.Window.DecorView.ClearFocus();
                }
            }
        }
    }
    

    3.in IOS

    public class iOSKeyboardHelper : IKeyboardHelper
    {
        public void HideKeyboard()
        {
            UIApplication.SharedApplication.KeyWindow.EndEditing(true);
        }
    }
    
    1. A simple usage
         private void MyEntry_TextChanged(object sender, TextChangedEventArgs e)
            {
                try
                {
                    if (e.NewTextValue!=null && e.NewTextValue.Length== 4) {
                        myEntry.Text = e.NewTextValue;
                        DependencyService.Get<IKeyboardHelper>().HideKeyboard();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception caught: {0}", ex);
                }
            }
    

    The result is:

    Note: As a test, when I entered four characters,I will hide the keyboard .You can change above code according to your requirement.

  • InfinityInfinity Member ✭✭
    edited August 6

    Thanks for you response! This works..
    However I wish to find out what is wrong in my current approach.
    I ended up adding this - base.OnFocusChangeRequested
    and removing - Control.ClearFocus();

    Which got it working.
    Although I couldn't understand what made it work .. Any idea.. ?
    Why calling the base method made it work..?

  • jezhjezh Member, Xamarin Team Xamurai

    I ended up adding this - base.OnFocusChangeRequested
    and removing - Control.ClearFocus();

    Hi @Infinity , I am confused with what you said. What method did you end up using?Could you please post the full code you used ?

Sign In or Register to comment.