Browse Source

implement return key and accepts return, and closing keyboard on done return keys.

Dan Walmsley 3 years ago
parent
commit
c82687426c

+ 2 - 2
samples/MobileSandbox/MainView.xaml

@@ -3,9 +3,9 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <StackPanel Margin="100 50" Spacing="50">
     <TextBlock Text="Login" Foreground="White" />
-    <TextBox Watermark="Username" TextInputOptions.ContentType="Email" />
+    <TextBox Watermark="Username" TextInputOptions.ContentType="Email" AcceptsReturn="True" TextInputOptions.ReturnKeyType="Search" />
     <TextBox Watermark="Password" PasswordChar="*" TextInputOptions.ContentType="Password" />
     <TextBox Watermark="Pin" PasswordChar="*" TextInputOptions.ContentType="Digits" />
-    <Button Content="Login" />
+    <Button Content="Login" Command="{Binding ButtonCommand}" />
   </StackPanel>
 </UserControl>

+ 9 - 0
samples/MobileSandbox/MainView.xaml.cs

@@ -1,3 +1,5 @@
+using System;
+using System.Windows.Input;
 using Avalonia.Controls;
 using Avalonia.Markup.Xaml;
 
@@ -8,6 +10,13 @@ namespace MobileSandbox
         public MainView()
         {
             AvaloniaXamlLoader.Load(this);
+
+            DataContext = this;
+        }
+
+        public void ButtonCommand()
+        {
+            Console.WriteLine("Button pressed");
         }
     }
 }

+ 16 - 1
src/iOS/Avalonia.iOS/TextInputResponder.Properties.cs

@@ -35,7 +35,22 @@ partial class AvaloniaView
         [Export("keyboardAppearance")]
         public UIKeyboardAppearance KeyboardAppearance => UIKeyboardAppearance.Alert;
 
-        [Export("returnKeyType")] public UIReturnKeyType ReturnKeyType => (UIReturnKeyType)(_view._options?.ReturnKeyType ?? TextInputReturnKeyType.Default);
+        [Export("returnKeyType")]
+        public UIReturnKeyType ReturnKeyType
+        {
+            get
+            {
+                if (_view._options != null)
+                {
+                    if (_view._options.ReturnKeyType == TextInputReturnKeyType.Default)
+                    {
+                        return _view._options.Multiline ? UIReturnKeyType.Default : UIReturnKeyType.Done;
+                    }
+                }
+
+                return UIReturnKeyType.Default;
+            }
+        } 
 
         [Export("enablesReturnKeyAutomatically")]
         public bool EnablesReturnKeyAutomatically { get; set; }

+ 10 - 0
src/iOS/Avalonia.iOS/TextInputResponder.cs

@@ -149,6 +149,16 @@ partial class AvaloniaView
             if (text == "\n")
             {
                 KeyPress(Key.Enter);
+
+                switch (ReturnKeyType)
+                {
+                    case UIReturnKeyType.Done:
+                        case UIReturnKeyType.Go:
+                        case UIReturnKeyType.Send:
+                        case UIReturnKeyType.Search:
+                        ResignFirstResponder();
+                        break;
+                }
                 return;
             }