2015-08-13 108 views
0

隱藏軟鍵盤很痛苦。 我使用了一些基於獲取焦點的EditText的方法,但在我的當前應用程序中,鍵盤會在加載新片段的某個點彈出。從任何地方隱藏軟鍵盤

我有一個方法,在我的助手類,但它不爲我工作:

//Hide keyboard 
    public static void hideSoftKeyboard(Activity activity) { 
     activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    } 

我會愛是一個輔助方法,我可以從任何地方調用來隱藏軟鍵盤。這是可能的,還是我總是需要找到關注的EditText?

+1

清單中'機器人試試這個:windowSoftInputMode =「stateHidden」' – Emil

回答

3

做這樣的事情傳遞activity..it任何的EditText ID將爲該activty

public static void hideSoftKeyboard(Activity activity, EditText editText) { 
     InputMethodManager imm = (InputMethodManager) activity.getSystemService(
       Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); 
    } 
+0

你的意思是,你可以添加一個虛擬/隱藏的EditText到活動並用它來隱藏鍵盤?即使它最初是由另一個EditText添加的? – TomCB

+0

是的.. !!! –

+0

這個技巧就像一個魅力!非常感謝! – TomCB

0

在AndroidManifest.xml中:

<activity android:name="com.your.package.ActivityName" 
      android:windowSoftInputMode="stateHidden" /> 

此設置在用戶進入新的活動將隱藏軟鍵盤(即使控制的EditText收益焦點)。軟鍵盤僅在用戶單擊編輯框控件時纔會顯示。

0

與此

public static void hideAllKeyboard(Activity activity) 
{ 
    View view = activity.getCurrentFocus(); 
    if (view != null) { 
     InputMethodManager imm = (InputMethodManager)activity.getSystemService(
       Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
    } 
} 
1

試試使用該代碼的工作(發現它在互聯網茹段Habra Habr

public void showSoftInputOnFocusCompat(boolean isShow) { 

    showSoftInputOnFocus = isShow; 
    if (Build.VERSION.SDK_INT >= 21) { 
     setShowSoftInputOnFocus(showSoftInputOnFocus); 
    } else { 
     try { 
      final Method method = EditText.class.getMethod("setShowSoftInputOnFocus", boolean.class); 
      method.setAccessible(true); 
      method.invoke(this, showSoftInputOnFocus); 
     } catch (Exception e) { 
      // ignore 
     } 
    } 
} 
0

使用本

public void hideSoftKeyboard(Activity context) 
{ 
    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); 
    inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0); 
} 
0

適合我這種方法:

* First create a derivated class from Entry 

    public class KBLessEntry : Entry 
    { 
    public KBLessEntry() : base() 
    { 
    } 
    } 

* Then create a custom platform EntryRender 

    using Xamarin.Forms.Platform.Android; 
    using Xamarin.Forms; 
    using MobileClients.Droid.Core; 
    using Android.Views.InputMethods; 
    using System; 
    using System.ComponentModel; 

[assembly: ExportRenderer(typeof(KBLessEntry), typeof(KBLessEntryRender))] 
namespace MobileClients.Droid.Core 
{ 
    public class KBLessEntryRender : EntryRenderer 
    { 
     protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) 
     { 
      base.OnElementPropertyChanged(sender, e); 
      Control.InputType = 0; 
      try 
      {    
       // Hide keyboard 
       InputMethodManager inputMethodManager = this.Control.Context.GetSystemService(Android.Content.Context.InputMethodService) as InputMethodManager; 
       if (inputMethodManager != null) 
       { 
        inputMethodManager.HideSoftInputFromWindow(this.Control.WindowToken, HideSoftInputFlags.None); 
       } 
      } 
      catch(Exception Ex) 
      { 

      } 
     } 

    } 
} 

而在XAML

<local:KBLessEntry x:Name="TxtCode" FontSize="18" Placeholder="Código producto" TextColor="Black" HorizontalOptions="FillAndExpand"></local:KBLessEntry> 

地方:必須定義在XAML中的xmlns的命名空間:地方=「CLR的命名空間:MobileClients .Droid.Core;程序集= MobileClients.Droid「

而那就是它

0

嘗試調用這個方法: setShowSoftInputOnFocus(false); 對我來說,它的工作好:

public class CustomKeyboardField extends android.support.v7.widget.AppCompatEditText { 


    public CustomKeyboardField(Context context) { 
     super(context); 
     init(); 
    } 

    public CustomKeyboardField(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public CustomKeyboardField(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    private void init() { 
     setShowSoftInputOnFocus(false); 
     setCursorVisible(false); 
    } 
}