我在 Objective-C 上找到了我的问题的答案,但我没有在 Xamarin iOS 中搜索它。 所以我有用户写电话号码的字段,当这个字段编辑出现键盘,数字键盘类型时。但是这个键盘没有隐藏,也没有隐藏按钮。
在这个问题的 android 应用程序中,我使用代码:
one.Click += delegate
{
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(ulitsa.WindowToken, 0);
};
在我的 iOS 应用程序中,此代码不起作用。 我在 iOS 中的代码:
tel.ShouldReturn = delegate {
tel.ResignFirstResponder ();
//tel.ReturnKeyType = UIReturnKeyType.Done;
return true;
};
此代码适用于默认键盘类型。在键盘类型的数字键盘中,我在屏幕截图中得到了结果:
我该如何解决我的问题?
请您参考如下方法:
您可以创建一个带有可在其他 ViewController 中使用的辅助方法的基类。
即:
/// <summary>
/// Base class for Controllers that inherit from UIViewController
/// </summary>
public class SimpleViewControllerBase : UIViewController
{
public SimpleViewControllerBase(IntPtr handle) : base(handle)
{
}
/* This will add "Done" button to numeric keyboard */
protected void AddDoneButtonToNumericKeyboard(UITextField textField)
{
UIToolbar toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f));
var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate
{
textField.ResignFirstResponder();
});
toolbar.Items = new UIBarButtonItem[] {
new UIBarButtonItem (UIBarButtonSystemItem.FlexibleSpace),
doneButton
};
textField.InputAccessoryView = toolbar;
}
}
然后在您的实际 ViewController 中,您可以执行如下操作:
public partial class ViewController : SimpleViewControllerBase
{
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
AddDoneButtonToNumericKeyboard("NumericTextFieldName");
}
}
当然,您必须在 Storyboard中定义数字文本字段的名称。
关键是要有像AddDoneButtonToNumericKeyboard()这样的函数
