Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close the keyboard once button pressed in Xamarin

In a view controller I have a 2 text boxes (UITextField) and a submit button. The text boxes pop up the ASCII keyboard. The Submit button takes the values from the text boxes and does something with them.

  1. When the keyboard is open, how do I kill it once the submit button is pressed
  2. The keyboard has a Next button, how do I get it to go to the next field.

Using Xamarin Studio 4.0.12

Thank you!

like image 275
user1229084 Avatar asked Mar 23 '23 09:03

user1229084


1 Answers

You need to do what incmiko suggested. Here's 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;
}
like image 92
Mohib Sheth Avatar answered Apr 26 '23 04:04

Mohib Sheth