使用 Monotouch 的 UITextField ResignFirstResponder 示例

发布于 2024-08-26 02:32:03 字数 220 浏览 7 评论 0原文

我是新手。当用户在 UITextField 中输入文本后,我不知道如何以及在哪里调用 ResignFirstResponder 来摆脱键盘。我对 UIResponder 类有点困惑。 Mono 文档说:“要关闭键盘,请将 UIResponder.ResignFirstResponder 消息发送到当前第一响应者的文本字段。”怎么办呢?有人可以发布一个简单的工作示例吗? Obj-C 中有很多示例,但 C# 中没有。非常感谢。

I'm a newbie. I can't figure out how and where to call ResignFirstResponder to get rid of the keyboard when the user finished entering the text in an UITextField. I'm a bit confused by the UIResponder class. Mono documentation says: "To dismiss the keyboard, send the UIResponder.ResignFirstResponder message to the text field that is currently the first responder." How to do so? Can someone post a simple working example? There are many examples in Obj-C but none in C#. Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2024-09-02 02:32:03

这是我最近完成的一个示例

private UITextField _textField;

public override void ViewDidLoad()
{
    _textField = new UITextField();
    _textField.Text = "King Alfonso III";
    _textField.Bounds = bounds;
    _textField.Placeholder = "Username";
    _textField.ShouldReturn = delegate
    {
        _textField.ResignFirstResponder();
        return true;
    };
    View.AddSubview(_textField);
}

此外,如果您提交带有按钮的表单,请确保放弃按钮单击中的所有文本字段,以避免出现响应者错误。

public void ButtonClick(object sender, EventArgs e)
{
    _textField.ResignFirstResponder();
    // All other textboxes

    // Other button logic
}

Here's an example I've done recently:

private UITextField _textField;

public override void ViewDidLoad()
{
    _textField = new UITextField();
    _textField.Text = "King Alfonso III";
    _textField.Bounds = bounds;
    _textField.Placeholder = "Username";
    _textField.ShouldReturn = delegate
    {
        _textField.ResignFirstResponder();
        return true;
    };
    View.AddSubview(_textField);
}

Also if you are submitted a form with a button, make sure you resign all the textfields in the button click, to avoid getting responder errors.

public void ButtonClick(object sender, EventArgs e)
{
    _textField.ResignFirstResponder();
    // All other textboxes

    // Other button logic
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文