2015-05-25 116 views
1

經過多次的狩獵之後,我找到了一種方法來將鍵盤隱藏在Xamarin Forms中的按鈕上,用於iOS的情況。所以分享如下。如何在Xamarin Forms中按下按鈕時關閉鍵盤

如果任何人都可以改進它,或分享一個Android方面的解決方案,這將是很好的。

回答

2

在共享/ PCL項目,添加:

using System; 
using Xamarin.Forms; 

namespace MyApp.Views 
{ 
    public class ButtonKeyboardHiding : Button {} 
} 

使用這個類來代替按鈕的表單。

在iOS的項目中,添加:

using System; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.iOS; 
using Foundation; 
using UIKit; 
using MyApp.Views; 
using MyApp.iOS; 

[assembly: ExportRenderer (typeof (ButtonKeyboardHiding), typeof (ButtonKeyboardHidingRenderer)) ] 

namespace MyApp.iOS 
{ 
    public class ButtonKeyboardHidingRenderer : ButtonRenderer 
    { 
     protected override void OnElementChanged (ElementChangedEventArgs<Button> e) 
     { 
      base.OnElementChanged (e); 

      if (Control != null) 
      { 
       Control.TouchUpInside += (sender, el) => 
       { 
        UIView ctl = Control; 
        while (true) 
        { 
         ctl = ctl.Superview; 
         if (ctl.Description.Contains ("UIView")) 
          break; 
        } 
        ctl.EndEditing (true); 
       }; 
      } 
     } 
    } 
} 
+0

顯得很荒謬,你無法通過上一個ListView的看法迭代。謝謝,這個想法。 –

+0

我試過了,但不起作用。當我第二次點擊按鈕時(鍵盤關閉後),我收到一個異常。 – jbyrd

5

這是我的解決方案,以隱藏虛擬鍵盤爲Android的一面。

我編寫了一個具有HideKeyboard()方法的IKeyboardInteractions接口。然後我聲明在MyProject.Droid一個KeyboardInteractions類,它實現IKeyboardInteractions:

普通的代碼:

public interface IKeyboardInteractions { 
    void HideKeyboard(); 
} 

MyProject.Droid代碼:

[assembly: Dependency (typeof (KeyboardInteractions))] namespace MyProject.Droid 
{ 
    public class KeyboardInteractions : IKeyboardInteractions 
    { 
     public void HideKeyboard() 
     { 
      var inputMethodManager = Xamarin.Forms.Forms.Context.GetSystemService(Context.InputMethodService) as InputMethodManager; 
      if (inputMethodManager != null && Xamarin.Forms.Forms.Context is Activity) 
      { 
       var activity = Xamarin.Forms.Forms.Context as Activity; 
       var token = activity.CurrentFocus == null ? null : activity.CurrentFocus.WindowToken; 
       inputMethodManager.HideSoftInputFromWindow(token, 0); 
      } 
     } 
    } 
} 

消費方法:

IKeyboardInteractions keyboardInteractions = DependencyService.Get<IKeyboardInteractions>(); 
keyboardInteractions.HideKeyboard(); 
+0

非常感謝尼古拉斯。將試圖走出軌道。 – BillF

4

我發現這個很有用:

https://forums.xamarin.com/discussion/comment/172077#Comment_172077

接口:

public interface IKeyboardHelper 
{ 
    void HideKeyboard(); 
} 

的iOS:

public class iOSKeyboardHelper : IKeyboardHelper 
{ 
    public void HideKeyboard() 
    { 
     UIApplication.SharedApplication.KeyWindow.EndEditing(true); 
    } 
} 

德羅伊德:

public class DroidKeyboardHelper : IKeyboardHelper 
{ 
    public void HideKeyboard() 
    { 
     var context = Forms.Context; 
     var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager; 
     if (inputMethodManager != null && context is Activity) 
     { 
      var activity = context as Activity; 
      var token = activity.CurrentFocus?.WindowToken; 
      inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None); 

      activity.Window.DecorView.ClearFocus(); 
     } 
    } 
} 

使用在Xamarin形式:

DependencyService.Get<IKeyboardHelper>().HideKeyboard(); 
+2

在每個KeyboardHelper類的名稱空間開始之前,您需要這一行,否則DependencyService將找不到它們。[assembly:Xamarin.Forms.Dependency(typeof(xxxxxxKeyboardHelper))] – JDibble

+0

KUDOS與F. Badili和JDibble都有。 – envyM6

相關問題