【问题标题】:How to hide number pad, I'm use Xamarin iOS (C#)如何隐藏数字键盘,我使用的是 Xamarin iOS (C#)
【发布时间】:2016-01-27 12:54:50
【问题描述】:

我找到了关于 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;
        };

此代码适用于默认键盘类型。在键盘类型的数字键盘中,我得到了截图:

我该如何解决我的问题?

【问题讨论】:

  • 这不是标准的方法。共同决定 - 导航面板上的完成按钮,或者您可以在键盘上添加自定义按钮。
  • 如何在键盘上添加按钮?也许你有例子?
  • 在objective-c上medium.com/@chan0123/…

标签: c# ios xamarin xamarin.ios


【解决方案1】:

您可以使用可在其他 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;
    }
}

然后在您的实际 ViewControllers 中,您可以执行以下操作:

public partial class ViewController : SimpleViewControllerBase
{
    public ViewController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        AddDoneButtonToNumericKeyboard("NumericTextFieldName");
    }
}

当然,您必须在情节提要中定义数字文本字段的名称。

关键是要有 AddDoneButtonToNumericKeyboard() 之类的函数

最终结果如下所示:

【讨论】:

  • 这个解决方案就像魅力一样,即使你有多个数字字段。谢谢老哥
【解决方案2】:

你可以看到这个blog。可能对你有很大帮助。

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

// Your stuff

NSNotificationCenter.DefaultCenter.AddObserver   ("UIKeyboardWillShowNotification", KeyboardWillShow);
}

 public void KeyboardWillShow(NSNotification notification)
 {
 var doneButton = new UIButton (UIButtonType.Custom);
 doneButton.Frame = new RectangleF (0, 163, 106, 53);
 doneButton.SetTitle ("DONE", UIControlState.Normal);
 doneButton.SetTitleColor (UIColor.Black, UIControlState.Normal);
 doneButton.SetTitleColor (UIColor.White, UIControlState.Highlighted);

 doneButton.TouchUpInside += (sender, e) => 
 {
 // Make the Done button do its thing!  The textfield shouldn't be the   first responder
 _txtNumbers.ResignFirstResponder();
 };

 // This is the 'magic' that could change with future version of iOS
 var keyboard = _txtNumbers.WeakInputDelegate as UIView;
 if (keyboard != null)
 {
     keyboard.AddSubview (doneButton);
 }
}

或者你也可以这个代码

 this.View.EndEditing(true);

【讨论】:

  • 这个变量“_txtNumbers”是什么类型的?
  • var _txtNumbers = new UITextField (new RectangleF (5, 50, 200, 20)); _txtNumbers.KeyboardType = UIKeyboardType.NumberPad; _txtNumbers.BorderStyle = UITextBorderStyle.RoundedRect; View.AddSubview(_txtNumbers);
  • 你可以在这里找到整个项目。 github.com/xeb/xamarin-done-button-example
  • 在这个字符串中 "NSNotificationCenter.DefaultCenter.AddObserver("UIKeyboardWillShowNotification", KeyboardWillShow);"我有 2 个例外 1)“错误 CS1502:Foundation.NSNotificationCenter.AddObserver(Foundation.NSString, System.Action&lt;Foundation.NSNotification&gt;)' has some invalid arguments (CS1502)" 2)"Error CS1503: Argument #1' 的最佳重载方法匹配无法转换 string' expression to type Foundation.NSString' (CS1503)”
  • 你是从 github 下载应用并测试的吗?
【解决方案3】:
猜你喜欢
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 2016-12-15
  • 2013-09-17
  • 2017-11-19
  • 2015-01-17
相关资源
最近更新 更多