【问题标题】:How to programmatically dismiss the keyboard in Monotouch如何以编程方式关闭 Monotouch 中的键盘
【发布时间】:2012-05-14 19:49:39
【问题描述】:

在本机 Objective-C 中有许多如何做到这一点的例子,但我似乎找不到如何在 MonoTouch 的 C# 代码中完成同样的例子。

我想关闭当用户按下返回键时弹出的键盘。

EditingDidEnd 是要捕获的正确事件吗?我似乎找不到解雇第一响应者方法调用。

高度赞赏。

【问题讨论】:

    标签: xamarin.ios


    【解决方案1】:

    查看here 以获取示例。

    一般来说,如果您可以链接到示例,会容易得多。人们会确切地看到您想要什么,并告诉您如何使用 MonoTouch 做到这一点。此外,提供您自己的部分/不工作的代码通常会很有帮助。

    【讨论】:

      【解决方案2】:

      Here is gist showing如何在数字键盘上方添加关闭按钮。你可以用 1LOC 做到这一点

      myTextField.SetKeyboardEditorWithCloseButton(UIKeyboardType.NumberPad); 
      

      使用此扩展方法:

      public static void SetKeyboardEditorWithCloseButton (this UITextField txt, UIKeyboardType     keyboardType, string closeButtonText = "Done")
      {
          UIToolbar toolbar = new UIToolbar ();
          txt.KeyboardType = keyboardType;
          toolbar.BarStyle = UIBarStyle.Black;
          toolbar.Translucent = true;
          toolbar.SizeToFit ();
          UIBarButtonItem doneButton = new UIBarButtonItem (closeButtonText, UIBarButtonItemStyle.Done,
                                                            (s, e) => {
              txt.ResignFirstResponder ();
          });
          toolbar.SetItems (new UIBarButtonItem[]{doneButton}, true);
      
          txt.InputAccessoryView = toolbar;
      }
      

      numeric keyboard with closing button http://moocode.net/img/numeric_editor.png

      【讨论】:

        【解决方案3】:

        在您的TouchesBegan() 方法中,只需像您通常那样为任何文本框调用textbox.ResignFirstReponder()View.EndEditing(true);。当然,这仅在您位于从 UIViewController 继承并具有这些事件的地方时。

        【讨论】:

        • 我捕获了 DidEndEditing 事件并在发件人中调用了 ResignFirstResponder()。这似乎可以解决问题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 2022-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 1970-01-01
        相关资源
        最近更新 更多