Close the keyboard as soon as the button is pressed in Xamarin

In the view controller, I have 2 text fields (UITextField) and a submit button. Text fields pop up on an ASCII keyboard. The Submit button accepts values ​​from text fields and does something with them.

  • When the keyboard is open, how to kill it after pressing the send button.
  • There is a "Next" button on the keyboard, how can I go to the next field.

Using Xamarin Studio 4.0.12

Thanks!

+4
source share
2 answers

You need to do what inmiko suggested. Here is the code in C #

Part 1.

txtUsername.ShouldReturn = TextFieldShouldReturn; txtPassword.ShouldReturn = TextFieldShouldReturn; 

create a function in your view

 private bool TextFieldShouldReturn(UITextField tf) { //change the code below as per your validation if (tf == _txtUsername) { _txtPassword.BecomeFirstResponder(); return true; } if(tf == _txtPassword) { // validate field inputs as per your requirement tf.ResignFirstResponder(); return true; } return true; } 
+7
source

Just try with it,

This is just a sample.

 NSObject keyboardShowObserver; NSObject keyboardHideObserver; public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); keyboardShowObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, (notification) => { NSValue nsKeyboardBounds = (NSValue)notification.UserInfo.ObjectForKey(UIKeyboard.BoundsUserInfoKey); RectangleF keyboardBounds = nsKeyboardBounds.RectangleFValue; float height = View.Bounds.Height - keyboardBounds.Height; if (NavigationController != null && NavigationController.TabBarController != null && NavigationController.TabBarController.TabBar != null) { // Re-add tab bar height since it is hidden under keyboard but still excluded from View.Bounds.Height. height += NavigationController.TabBarController.TabBar.Frame.Height; } someScrollView.Frame = new RectangleF(someScrollView.Frame.Location, new SizeF(View.Bounds.Width, height)); }); keyboardHideObserver = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, (notification) => { UIApplication.EnsureUIThread(); someScrollView.Frame = new RectangleF(someScrollView.Frame.Location, View.Bounds.Size); }); } public override void ViewDidDisappear(bool animated) { base.ViewDidDisappear(animated); if (keyboardShowObserver != null) { NSNotificationCenter.DefaultCenter.RemoveObserver(keyboardShowObserver); } if (keyboardHideObserver != null) { NSNotificationCenter.DefaultCenter.RemoveObserver(keyboardHideObserver); } } 
0
source

Source: https://habr.com/ru/post/1502701/


All Articles