我触发条目的onfocus代码,但Android中的隐藏代码并不隐藏软键盘。
public interface IKeyboardService
{
void Hide();
void Show();
}
public void OnFocused()
{
var serv = DependencyService.Get<IKeyboardService>();
if (cp.FindByName<SwitchCell>("SCell").On)
{
serv.Show();
}
else
{
serv.Hide();
}
}
[assembly: Dependency(typeof(KeyboardService))]
namespace KeyboardHide.Droid.DependencyServices
{
public class KeyboardService : IKeyboardService
{
public void Hide()
{
var inputMethodManager = Xamarin.Forms.Forms.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null && Xamarin.Forms.Forms.Context is Activity)
{
var activity = Xamarin.Forms.Forms.Context as Activity;
var focusedView = activity.CurrentFocus;
var token = focusedView == null ? null : focusedView.WindowToken;
inputMethodManager.HideSoftInputFromInputMethod(token, HideSoftInputFlags.None);
}
}
public void Show()
{
}
}
}
我也试过这段代码
var inputMethodManager = Xamarin.Forms.Forms.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null && Xamarin.Forms.Forms.Context is Activity)
{
var activity = Xamarin.Forms.Forms.Context as Activity;
var focusedView = activity.CurrentFocus;
var token = focusedView == null ? null : focusedView.WindowToken;
inputMethodManager.ToggleSoftInputFromWindow(token, ShowSoftInputFlags.None, HideSoftInputFlags.None);
}
这对我来说是另一个项目吗?
*编辑:
以前的项目(为我的另一个项目工作):
var inputMethodManager = Xamarin.Forms.Forms.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null && Xamarin.Forms.Forms.Context is Activity)
{
var activity = Xamarin.Forms.Forms.Context as Activity;
var focusedView = activity.CurrentFocus;
var token = focusedView == null ? null : focusedView.WindowToken;
inputMethodManager.ToggleSoftInputFromWindow(token, ShowSoftInputFlags.None, HideSoftInputFlags.None);
}
但是对于我的新项目,它并不起作用。我仍然看到键盘,它并没有隐藏它,我也试过了:
Device.BeginInvokeOnMainThread(() => serv.Hide());
答案 0 :(得分:0)
以下Android代码适用于我(从其他人那里获取此代码但不记得现在在哪里,我假设Xamarin论坛)。它具有能够被await
编辑的附加好处,但如果您不想这样做,则可以删除TaskCompletionSource
内容:
public Task<bool> DismissKeyboardAsync() {
TaskCompletionSource<bool> result = new TaskCompletionSource<bool>();
try {
View view = ((MainActivity)Forms.Context).CurrentFocus;
IBinder windowToken = view?.WindowToken;
if(windowToken != null) {
((InputMethodManager)Forms.Context.GetSystemService(Context.InputMethodService)).HideSoftInputFromWindow(windowToken, HideSoftInputFlags.None);
result.TrySetResult(true);
return result.Task;
}
result.TrySetResult(false);
} catch(System.Exception exception) {
System.Console.WriteLine("\nIn NativeHelper_Android.DismissKeyboardAsync() - Exception:\n{0}\n", exception);
result.TrySetException(exception);
result.TrySetResult(false);
}
return result.Task;
}
没有async
的示例可能是(这是未经测试的代码):
public void Hide() {
try {
View view = ((MainActivity)Forms.Context).CurrentFocus;
IBinder windowToken = view?.WindowToken;
if(windowToken != null) {
((InputMethodManager)Forms.Context.GetSystemService(Context.InputMethodService)).HideSoftInputFromWindow(windowToken, HideSoftInputFlags.None);
}
} catch(System.Exception exception) {
System.Console.WriteLine("\nIn NativeHelper_Android.DismissKeyboardAsync() - Exception:\n{0}\n", exception);
}
}