I couldn't find anything in the Xamarin documentation about navigating to the next text field in a series of text fields on a form, only a small tutorial on removing the keyboard.
To keep things simple I am using a txtUsername(tag 1) text field and a txtPassword(tag 2) text field. When I implement the following code, it isn't transferring to Xamarin Studio. Does anyone know the way this can be done code in Xamarin Studio, or alternatively if I can transfer the code from XCode to Xamarin Studio.
I am using the following code:
- (BOOL)textFieldShouldReturn:(UITextField *)txtUsername{
NSLog(@"textFieldShouldReturn:");
if (txtUsername.tag == 1) {
UITextField *txtPassword= (UITextField *)[self.view viewWithTag:2];
[txtPassword becomeFirstResponder];
}
else {
[txtUsername resignFirstResponder];
}
return YES;
}
Thanks in advance
I think that the simplest way to do this is using the ShouldReturn method on your UITextFields with the BecomeFirstResponder method. For example, for a login form with Username and Password UITextFields, as follows (in your ViewDidLoad method):
Username = new UITextField();
Username.ReturnKeyType = UIReturnKeyType.Next;
Username.KeyboardType = UIKeyboardType.EmailAddress;
Username.ShouldReturn = (tf) =>
{
Password.BecomeFirstResponder();
return true;
};
View.Add(Username);
Password = new UITextField();
Password.SecureTextEntry = true;
Password.ReturnKeyType = UIReturnKeyType.Go;
Password.ShouldReturn = (tf) =>
{
// Do your login
return true;
};
View.Add(Password);
When you click next when the Username is active, it moves to the Password field. Clicking Go in the password field submits the form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With