Xamarin.Forms Entry That Should Not Show SoftKeyboard on Its Focus

◇◆丶佛笑我妖孽 提交于 2019-12-13 03:49:08

问题


I am working on Xamarin Forms project and I am using Entry view and that is requirement since i need to be able to focus on it but it is also requirement to not show soft keyboard.

This requirement cannot be changed. Also, I cannot use Label or Button instead of Entry since these do not receive focus.

Following this article (https://theconfuzedsourcecode.wordpress.com/2017/05/19/a-keyboard-disabled-entry-control-in-xamarin-forms/comment-page-1/#comment-1300), I tried creating custom renderer and using ShowSoftInputOnFocus on Android but that will briefly show, then hide soft keyboard. This is not an option since my requirement is strict to not show soft keyboard on this custom Entry field at all.

So, I created my custom KeyboardlessEntry in Xamarin.Forms project (.NET Standard 2.0):

namespace MyProjNamespace
{
    public class KeyboardlessEntry : Entry
    {
    }
}

, and then my custom KeyboardlessEntryRenderer renderer in Xamarin.Droid head project like so:

[assembly: ExportRenderer(typeof(KeyboardlessEntry), typeof(KeyboardlessEntryRenderer))]
namespace MyProjNamespace.Droid
{
    public class KeyboardlessEntryRenderer : EntryRenderer
    {
        //as of latest Xamarin.Forms need to provide c-tor that
        //receives Android Context and sets it in base
        public KeyboardlessEntryRenderer (Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                ((KeyboardlessEntry)e.NewElement).PropertyChanging += OnPropertyChanging;
            }

            if (e.OldElement != null)
            {
                ((KeyboardlessEntry)e.OldElement).PropertyChanging -= OnPropertyChanging;
            }

            this.Control.ShowSoftInputOnFocus = false; // disable soft keyboard on focus
        }

        private void OnPropertyChanging(object sender, PropertyChangingEventArgs propertyChangingEventArgs)
        {
            if (propertyChangingEventArgs.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
            {
                // fully dismiss the soft keyboard 
                InputMethodManager imm = (InputMethodManager)this.Context.GetSystemService(Context.InputMethodService);
                imm.HideSoftInputFromWindow(this.Control.WindowToken, 0);
            }
        }
    }
}

As you can see, I am setting ShowSoftInputOnFocus in OnElementChanged override but that does not prevent it from showing for some reason. My keyboard still shows on KeyboardlessEntry Focus and then disappears because I call HideSoftInputFromWindow inside OnPropertyChanging event.

I am not sure why this is not working. I would expect that setting ShowSoftInputOnFocus to false like I do above would disable soft keyboard from showing entirely. Some people claim that this works on Android or Xamarin.Android but it does not work in Xamarin.Forms.

Similar issue on iOS, here is the renderer for iOS

[assembly: ExportRenderer(typeof(KeyboardlessEntry), typeof(KeyboardlessEntryRenderer))] namespace Squirrel.FoH.App.iOS.Implementations.Controls { public class KeyboardlessEntryRenderer : EntryRenderer { public KeyboardlessEntryRenderer() { }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        this.Control.InputView = new UIView(); // disable soft keyboard
    }
}

}

This also shows, then reduces keyboard to strip below but note that there is no button to close it entirely making it even more annoying


回答1:


Try adding this inside your KeyboardlessEntryRenderer class:

public KeyboardlessEntryRenderer (Context context) : base(context)
{
}


来源:https://stackoverflow.com/questions/53490262/xamarin-forms-entry-that-should-not-show-softkeyboard-on-its-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!