在Xamarin中按一下按钮关闭键盘

时间:2013-09-17 22:59:16

标签: ios xamarin.ios keyboard

在视图控制器中,我有2个文本框(UITextField)和一个提交按钮。文本框弹出ASCII键盘。 “提交”按钮从文本框中获取值并对其执行某些操作。

  1. 当键盘打开时,如何在按下提交按钮后将其杀死
  2. 键盘上有一个下一个按钮,如何让它转到下一个字段。
  3. 使用Xamarin Studio 4.0.12

    谢谢!

2 个答案:

答案 0 :(得分:7)

你需要做incmiko建议的事情。这是C#中的代码

第1部分。

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

在视图中创建一个功能

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;
}

答案 1 :(得分:0)

试试吧,

这只是一个样本,

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);
    }
}