Hide Soft Keyboard from anywhere

半腔热情 提交于 2019-12-25 03:11:46

问题


Hiding the soft keyboard is pain. I use some methods based on having an EditText which gets focus, but in my current app the keyboard keeps popping up at some point where a new fragment is loaded.

I have a method in my helper class, but it does not work for me:

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

What I would love is a helper method, I could call from anywhere to hide the soft keyboard. Is this possible, or do I always need the find the EditText which is focused?


回答1:


Do something like this pass any edittext id of that activity..it will work for that activty

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



回答2:


Try use that code (found it in ru internet segment 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
        }
    }
}



回答3:


In your AndroidManifest.xml:

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

This setting will hide soft keyboard when user enters new Activity (even if EditText control gains the focus). Soft keyboard will be shown only when user clicks the edit box control.




回答4:


Try with this

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



回答5:


use this

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



回答6:


for me works this method:

* 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)
            {

            }
        }

    }
}

And in XAML

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

local: must be defined has an namespace in your xaml xmlns:local="clr-namespace:MobileClients.Droid.Core;assembly=MobileClients.Droid"

And thats it




回答7:


Try call this method: setShowSoftInputOnFocus(false); In my case it work good:

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


来源:https://stackoverflow.com/questions/31984291/hide-soft-keyboard-from-anywhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!