C# Xamarin.Forms。安卓Can';隐藏键盘
当我的输入控件获得焦点时,会出现一个虚拟键盘。我需要隐藏键盘并仅在用户显式调用时显示它。我试图使用C# Xamarin.Forms。安卓Can';隐藏键盘,c#,xamarin,xamarin.forms,xamarin.android,C#,Xamarin,Xamarin.forms,Xamarin.android,当我的输入控件获得焦点时,会出现一个虚拟键盘。我需要隐藏键盘并仅在用户显式调用时显示它。我试图使用InputMethodManager隐藏键盘,但在调用HideSoftInputFromWindow(…)方法后,键盘仍保留在屏幕上ToggleSoftInputFromWindow(…)方法也不起作用 .NET标准项目: public partial class App : Application { public IKeyboardService KeyboardService { ge
InputMethodManager
隐藏键盘,但在调用HideSoftInputFromWindow(…)
方法后,键盘仍保留在屏幕上ToggleSoftInputFromWindow(…)
方法也不起作用
.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();
}
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 );
}
}
}
[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;
}
}
}
}
安卓项目:
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();
}
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 );
}
}
}
[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;
}
}
}
}
如何隐藏键盘 您可以在Xamarin.Forms中通过编程方式隐藏键盘,方法是在
条目上切换IsEnabled
公共类CustomEntry
{
公共报关单()
{
初始化组件();
聚焦+=一次定制聚焦;
}
CustomEntryFocused上的私有void(对象发送方、FocusEventArgs e)
{
var条目=(条目)发送方;
entry.IsEnabled=false;
entry.IsEnabled=true;
}
}
您可以通过在条目上切换IsEnabled
以编程方式在Xamarin.Forms中隐藏键盘
公共类CustomEntry
{
公共报关单()
{
初始化组件();
聚焦+=一次定制聚焦;
}
CustomEntryFocused上的私有void(对象发送方、FocusEventArgs e)
{
var条目=(条目)发送方;
entry.IsEnabled=false;
entry.IsEnabled=true;
}
}
有一种方法与上面Brandon Minnick所说的类似,使用IsEnable
属性禁用其焦点
以下是另一种方法,您应该在Android项目中创建自定义渲染器:
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();
}
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 );
}
}
}
[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;
}
}
}
}
在您的page.xaml中:
<local:CustomEntry Placeholder="hi" ></local:CustomEntry>
您将看到,当您关闭输入时,它不会弹出键盘。有一种方法类似于Brandon Minnick上面所说的,使用IsEnable
属性禁用其焦点
以下是另一种方法,您应该在Android项目中创建自定义渲染器:
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();
}
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 );
}
}
}
[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;
}
}
}
}
在您的page.xaml中:
<local:CustomEntry Placeholder="hi" ></local:CustomEntry>
您将看到,当您关闭输入时,它不会弹出键盘。如果要在活动开始时隐藏键盘,请在活动中添加以下内容:
WindowsofInputMode=Android.Views.SoftInput.StateAllwaysHidden
要以编程方式隐藏键盘,请使用此函数:
private void DismissKeyboard()
{
var view = CurrentFocus;
if (view != null)
{
var imm = (InputMethodManager)GetSystemService(InputMethodService);
imm.HideSoftInputFromWindow(view.WindowToken, 0);
}
}
private void DismissKeyboard2()
{
var currentFocus = this.CurrentFocus;
if (currentFocus != null)
{
InputMethodManager inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.None);
}
}
或此功能:
private void DismissKeyboard()
{
var view = CurrentFocus;
if (view != null)
{
var imm = (InputMethodManager)GetSystemService(InputMethodService);
imm.HideSoftInputFromWindow(view.WindowToken, 0);
}
}
private void DismissKeyboard2()
{
var currentFocus = this.CurrentFocus;
if (currentFocus != null)
{
InputMethodManager inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.None);
}
}
或者一起使用主题,如:
myEditText.FocusChange += (object sender, View.FocusChangeEventArgs e) =>
{
DismissKeyboard();
DismissKeyboard2();
};
如果要在活动开始时隐藏键盘,请在活动中添加以下内容:
WindowsofInputMode=Android.Views.SoftInput.StateAllwaysHidden
要以编程方式隐藏键盘,请使用此函数:
private void DismissKeyboard()
{
var view = CurrentFocus;
if (view != null)
{
var imm = (InputMethodManager)GetSystemService(InputMethodService);
imm.HideSoftInputFromWindow(view.WindowToken, 0);
}
}
private void DismissKeyboard2()
{
var currentFocus = this.CurrentFocus;
if (currentFocus != null)
{
InputMethodManager inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.None);
}
}
或此功能:
private void DismissKeyboard()
{
var view = CurrentFocus;
if (view != null)
{
var imm = (InputMethodManager)GetSystemService(InputMethodService);
imm.HideSoftInputFromWindow(view.WindowToken, 0);
}
}
private void DismissKeyboard2()
{
var currentFocus = this.CurrentFocus;
if (currentFocus != null)
{
InputMethodManager inputMethodManager = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.None);
}
}
或者一起使用主题,如:
myEditText.FocusChange += (object sender, View.FocusChangeEventArgs e) =>
{
DismissKeyboard();
DismissKeyboard2();
};
不幸的是,这对我不起作用。我可以完全禁用键盘,但我只需要在用户明确请求时显示键盘。不幸的是,这对我不起作用。我可以完全禁用键盘,但我只需要在用户明确请求时显示键盘。您从哪里获得GetSystemService
和WindowToken
?他们给了我一个错误。它们来自图书馆吗?您从哪里获得的GetSystemService
和WindowToken
?他们给了我一个错误。他们是从图书馆来的吗?