Browse Source

Android: remove input focus on soft keyboard hidden

ili 4 năm trước cách đây
mục cha
commit
cb22388de7

+ 2 - 1
src/Android/Avalonia.Android/AndroidInputMethod.cs

@@ -25,6 +25,7 @@ namespace Avalonia.Android
 
             _host.Focusable = true;
             _host.FocusableInTouchMode = true;
+            _host.ViewTreeObserver.AddOnGlobalLayoutListener(new SoftKeyboardListner(_host));
         }
 
         public void Reset()
@@ -84,7 +85,7 @@ namespace Avalonia.Android
                     outAttrs.InputType |= global::Android.Text.InputTypes.TextFlagMultiLine;
             });
 
-            _inputElement.PointerReleased += RestoreSoftKeyboard;
+            //_inputElement.PointerReleased += RestoreSoftKeyboard;
         }
 
         private void RestoreSoftKeyboard(object sender, PointerReleasedEventArgs e)

+ 42 - 0
src/Android/Avalonia.Android/SoftKeyboardListner.cs

@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Android.Content;
+using Android.OS;
+using Android.Util;
+using Android.Views;
+using Avalonia.Input;
+
+namespace Avalonia.Android
+{
+    class SoftKeyboardListner : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener
+    {
+        private const int DefaultKeyboardHeightDP = 100;
+        private static readonly int EstimatedKeyboardDP = DefaultKeyboardHeightDP + (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop ? 48 : 0);
+
+        private readonly View _host;
+        private bool _wasKeyboard;
+
+        public SoftKeyboardListner(View view)
+        {
+            _host = view;
+        }
+
+        public void OnGlobalLayout()
+        {
+            int estimatedKeyboardHeight = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip,
+                EstimatedKeyboardDP, _host.Resources.DisplayMetrics);
+
+            var rect = new global::Android.Graphics.Rect();
+            _host.GetWindowVisibleDisplayFrame(rect);
+
+            int heightDiff = _host.RootView.Height - (rect.Bottom - rect.Top);
+            var isKeyboard = heightDiff >= estimatedKeyboardHeight;
+
+            if (_wasKeyboard && !isKeyboard)
+                KeyboardDevice.Instance.SetFocusedElement(null, NavigationMethod.Unspecified, KeyModifiers.None);
+
+            _wasKeyboard = isKeyboard;
+        }
+    }
+}

+ 3 - 3
src/Android/Avalonia.AndroidTestApplication/MainActivity.cs

@@ -16,18 +16,18 @@ namespace Avalonia.AndroidTestApplication
         Icon = "@drawable/icon",
         LaunchMode = LaunchMode.SingleInstance/*,
         ScreenOrientation = ScreenOrientation.Landscape*/)]
-    public class MainBaseActivity : Activity
+    public class MainBaseActivity : AvaloniaActivity
     {
         protected override void OnCreate(Bundle savedInstanceState)
         {
-            base.OnCreate(savedInstanceState);
             if (Avalonia.Application.Current == null)
             {
                 AppBuilder.Configure<App>()
                     .UseAndroid()
                     .SetupWithoutStarting();
             }
-            SetContentView(new AvaloniaView(this) { Content = App.CreateSimpleWindow() });
+            base.OnCreate(savedInstanceState);
+            Content = App.CreateSimpleWindow();
         }
     }