2012-05-14 41 views
3

有許多關於如何在本機Objective-C中執行此操作的示例,但似乎無法找到如何在MonoTouch中的C#代碼中實現相同的示例。如何以編程方式關閉Monotouch中的鍵盤

我想關閉用戶按下返回鍵時彈出的鍵盤。

Is EditingDidEnd正確的事件陷阱?我似乎無法找到解僱第一響應者方法調用。

幫助高度讚賞。

回答

3

查看here的樣本。

一般來說,如果您可以鏈接到樣本,則會更容易。人們將會看到你想要的東西,並告訴你如何使用MonoTouch。同時提供您自己的部分/不合作代碼通常是一個很大的幫助。

0

在您的TouchesBegan()方法中,只需撥打textbox.ResignFirstReponder()即可像任何文本框一樣使用,或撥打View.EndEditing(true);。當然,這隻有在你是從UIViewController繼承並且有這些事件的地方。

+0

我被困在DidEndEditing事件,並呼籲ResignFirstResponder()發件人。這似乎是訣竅。 – rams

3

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