Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从任何地方隐藏软键盘_Android_Android Softkeyboard - Fatal编程技术网

Android 从任何地方隐藏软键盘

Android 从任何地方隐藏软键盘,android,android-softkeyboard,Android,Android Softkeyboard,隐藏软键盘是痛苦的。 我使用了一些基于EditText获得焦点的方法,但在我当前的应用程序中,键盘总是在加载新片段的某个点弹出 我的助手类中有一个方法,但它不适用于我: //Hide keyboard public static void hideSoftKeyboard(Activity activity) { activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STA

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

我的助手类中有一个方法,但它不适用于我:

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

我喜欢的是一个助手方法,我可以从任何地方调用来隐藏软键盘。这是可能的,还是我总是需要在您的AndroidManifest.xml中找到聚焦的编辑文本?

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


当用户输入新活动时,此设置将隐藏软键盘(即使EditText控件获得焦点)。只有当用户单击编辑框控件时,才会显示软键盘。

在AndroidManifest.xml中:

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


当用户输入新活动时,此设置将隐藏软键盘(即使EditText控件获得焦点)。只有当用户单击编辑框控件时,软键盘才会显示。

执行类似操作,传递该活动的任何edittext id。。它将适用于该活动

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

执行如下操作传递该活动的任何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);
    }
}
试试这个

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 internet段中找到)

尝试使用该代码(在ru internet段中找到)

用这个

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

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”


这就是我的工作方法:

* 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”

就是这样

尝试调用此方法:
setShowSoftInputFocus(假)
就我而言,它工作得很好:

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);
    }
}
尝试调用此方法:
setShowSoftInputFocus(假)
就我而言,它工作得很好:

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

在manifest
android:windowSoftInputMode=“stateHidden”
中尝试此操作在manifest
android:windowSoftInputMode=“stateHidden”
中尝试此操作您的意思是可以向活动添加虚拟/隐藏的编辑文本,并使用它隐藏键盘?即使它最初是由另一个EditText添加的?您的意思是可以将虚拟/隐藏的EditText添加到活动中并使用它隐藏键盘?即使它最初是由另一个EditText添加的?