从任何地方隐藏软键盘

Hide Soft Keyboard from anywhere

隐藏软键盘很痛苦。 我使用了一些基于获得焦点的 EditText 的方法,但在我当前的应用程序中,键盘在加载新片段的某个点不断弹出。

我的助手 class 中有一个方法,但它对我不起作用:

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

我想要的是一个辅助方法,我可以从任何地方调用来隐藏软键盘。这可能吗,还是我总是需要找到聚焦的 EditText?

在你的AndroidManifest.xml中:

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

此设置将在用户输入新内容时隐藏软键盘Activity(即使 EditText 控件获得焦点)。仅当用户单击编辑框控件时才会显示软键盘。

做这样的事情传递那个 activity 的任何 edittext id..它会为那个活动工作

public static void hideSoftKeyboard(Activity activity, EditText editText) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 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);
    }
}

尝试使用该代码(在 ru 互联网段 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
        }
    }
}

使用这个

public void hideSoftKeyboard(Activity context) 
{
    InputMethodManager inputMethodManager = (InputMethodManager)  context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 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>

local: 必须定义在你的 xaml xmlns:local="clr-namespace:MobileClients.Droid.Core;assembly=MobileClients.Droid"

中有一个命名空间

就是这样

尝试调用此方法: 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);
    }
}