简体   繁体   English

Xamarin.Forms。 安卓。 无法隐藏键盘

[英]Xamarin.Forms. Android. Can't hide keyboard

When my Entry control get focus a virtual keyboard appears.当我的 Entry 控件获得焦点时,会出现一个虚拟键盘。 I need to hide keyboard and show it only when user explicitly calls.我需要隐藏键盘并仅在用户明确调用时显示它。 I tried to use InputMethodManager to hide keyboard, but after calling the HideSoftInputFromWindow(...) method, keyboard still remains on screen.我尝试使用InputMethodManager隐藏键盘,但是在调用HideSoftInputFromWindow(...)方法后,键盘仍然保留在屏幕上。 ToggleSoftInputFromWindow(...) method also does not work. ToggleSoftInputFromWindow(...)方法也不起作用。

.NET Standard project: .NET 标准项目:

public partial class App : Application
{
    public IKeyboardService KeyboardService { get; set; }

    public App()
    {
        InitializeComponent();
        MainPage = new MainPage( new MainViewModel() );
    }
}

public partial class CustomEntry
{
    public CustomEntry()
    {
        InitializeComponent();
        Focused += OnCustomEntryFocused;
    }

    private void OnCustomEntryFocused( object sender, FocusEventArgs e )
    {
        ( (App)App.Current ).KeyboardService.HideKeyboard();
    }
}

public interface IKeyboardService
{
    void HideKeyboard();
}

Android project:安卓项目:

public class KeyboardService : IKeyboardService
{
    public void HideKeyboard()
    {
        var activity = MainActivity.Instance;
        var imm = activity.GetSystemService( Context.InputMethodService ) as InputMethodManager;
        if ( imm != null )
        {
            var token = activity.CurrentFocus?.WindowToken;
            imm.HideSoftInputFromWindow( token, HideSoftInputFlags.NotAlways );
            //imm.ToggleSoftInputFromWindow( token, ShowSoftInputFlags.None, HideSoftInputFlags.NotAlways );
        }
    }
}

How can I hide keyboard?如何隐藏键盘?

You can hide the keyboard programmatically in Xamarin.Forms by toggling IsEnabled on the Entry您可以通过在Entry上切换IsEnabled以编程方式在 Xamarin.Forms 中隐藏键盘

public class CustomEntry
{
    public CustomEntry()
    {
        InitializeComponent();
        Focused += OnCustomEntryFocused;
    }

    private void OnCustomEntryFocused(object sender, FocusEventArgs e)   
    {
        var entry = (Entry)sender;

        entry.IsEnabled = false;
        entry.IsEnabled = true;
    }
}

One method is like Brandon Minnick said above,use the IsEnable property to disable its focus.一种方法就像上面 Brandon Minnick 所说的,使用IsEnable属性来禁用它的焦点。

here is another method,that you should create a custom renderer in your Android project:这是另一种方法,您应该在您的 Android 项目中创建一个自定义渲染器:

[assembly: ExportRenderer(typeof(CustomEntry), typeof(MyEntryRenderer))]
namespace EntryCa.Droid
{
  class MyEntryRenderer:EntryRenderer
  {
    public MyEntryRenderer(Context context) : base(context)
    {

    }
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            // do whatever you want to the UITextField here!
            Control.InputType = Android.Text.InputTypes.Null;
        }
    }
  }
}

then in your PLC project :然后在您的 PLC 项目中:

public partial class CustomEntry :Entry
{

}

in your page.xaml :在您的 page.xaml 中:

<local:CustomEntry Placeholder="hi" ></local:CustomEntry>

you will see when you fouse the Entry,it will not popup the keyboard.您会看到当您对 Entry 进行融合时,它不会弹出键盘。

If you want to hide the keyboard when activity start , in your activity add this:如果您想在活动开始时隐藏键盘,请在您的活动中添加以下内容:

WindowSoftInputMode = Android.Views.SoftInput.StateAlwaysHidden WindowSoftInputMode = Android.Views.SoftInput.StateAlwaysHidden

[Activity(Label = "MainActivity", WindowSoftInputMode = Android.Views.SoftInput.StateAlwaysHidden)]

    public class MainActivity : Activity
    {  }

To hide the keyboard programmatically use this function:要以编程方式隐藏键盘,请使用此功能:

private void DismissKeyboard()
        {
            var view = CurrentFocus;
            if (view != null)
            {
                var imm = (InputMethodManager)GetSystemService(InputMethodService);
                imm.HideSoftInputFromWindow(view.WindowToken, 0);
            }
        }

Or this function:或者这个功能:

private void DismissKeyboard2()
        {
            var currentFocus = this.CurrentFocus;
            if (currentFocus != null)
            {
                InputMethodManager inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
                inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.None);
            }

        }

Or use theme together like:或者一起使用主题,例如:

myEditText.FocusChange += (object sender, View.FocusChangeEventArgs e) =>
            {

                DismissKeyboard();
                DismissKeyboard2();
            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM