簡體   English   中英

如何隱藏數字鍵盤,我正在使用Xamarin iOS(C#)

[英]How to hide number pad, I'm use Xamarin iOS (C#)

我找到了關於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;
    }
}

然后,在實際的ViewControllers中,您可以執行以下操作:

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

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

        AddDoneButtonToNumericKeyboard("NumericTextFieldName");
    }
}

當然,您必須在情節提要中定義數字文本字段的名稱。

關鍵是要具有類似AddDoneButtonToNumericKeyboard()的功能

最終結果如下所示: 在此處輸入圖片說明

您可以看到此博客 這可能對您有很大幫助。

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM