4

在视图控制器中,我有 2 个文本框(UITextField)和一个提交按钮。文本框弹出 ASCII 键盘。Submit 按钮从文本框中获取值并对其进行处理。

  1. 键盘打开时,按下提交按钮后如何将其杀死
  2. 键盘有一个下一步按钮,我如何让它进入下一个字段。

使用 Xamarin Studio 4.0.12

谢谢!

4

2 回答 2

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;
}
于 2013-09-19T07:04:39.810 回答
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);
    }
}
于 2015-10-27T06:07:39.153 回答