【发布时间】:2020-02-26 16:10:43
【问题描述】:
创建一个点击 web 视图输入字段的应用程序,必须执行操作。捕获并启动选定的操作可以正常工作,但由于它是通过单击输入字段启动的,因此需要使用键盘。在 Android
我已经尝试了所有被认为是post 的最佳答案的庄园或组合,但没有一个适用于我在 Android 9 上的应用
下面是我的 MainActivity 中的一些代码,其中创建了我的键盘服务实现的实例。 MainActivity 代码之后是我为 android 制作的键盘服务实现。
[Activity(Label = "Dental.App", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ScreenOrientation = ScreenOrientation.SensorLandscape,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, WindowSoftInputMode = SoftInput.StateAlwaysHidden) ]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
...
DependencyService.Get<IServiceCollection>().SetKeyboardService(new KeyboardService(this, GetInputMethodManager()));
...
}
public InputMethodManager GetInputMethodManager()
{
return (InputMethodManager)GetSystemService(Context.InputMethodService);
}
}
public class KeyboardService : IKeyboardService
{
private InputMethodManager inputMethodManager;
private readonly object mainActivity;
public KeyboardService(object activity, InputMethodManager methodManager)
{
mainActivity = activity;
inputMethodManager = methodManager;
}
public bool IsKeyboardShown => inputMethodManager.IsAcceptingText;
public void HideKeyboard()
{
if (inputMethodManager == null || !(mainActivity is Activity activity)) return;
Logging.Log(LogType.Information, $"Attempting to Hide Keyboard via 1st method...");
//var view = activity.CurrentFocus;
var view = activity.FindViewById(Android.Resource.Id.Content).RootView;
if (view == null) Logging.Log(LogType.Warning, $"Failed to get View from Activity...");
var token = view?.WindowToken;
if (token == null) Logging.Log(LogType.Warning, $"Failed to get Token from View...");
var success = inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
Logging.Log(LogType.Information,
$"{nameof(inputMethodManager.HideSoftInputFromWindow)} returned => {success}");
if(success) view?.ClearFocus();
if (!IsKeyboardShown)
{
view?.ClearFocus();
return;
}
Logging.Log(LogType.Warning,
$"Failed to Hide Keyboard via {nameof(inputMethodManager.HideSoftInputFromWindow)}...");
HideKeyboardAttemptTwo(activity);
}
private void HideKeyboardAttemptTwo(Activity activity)
{
Logging.Log(LogType.Information, $"Attempting to Hide Keyboard via 2nd method...");
//var view = activity.CurrentFocus;
var view = activity.FindViewById(Android.Resource.Id.Content).RootView;
if (view == null) Logging.Log(LogType.Warning, $"Failed to get View from Activity...");
var token = view?.WindowToken;
if (token == null) Logging.Log(LogType.Warning, $"Failed to get Token from View...");
inputMethodManager.ToggleSoftInputFromWindow(token, ShowSoftInputFlags.None, HideSoftInputFlags.None);
if (!IsKeyboardShown)
{
view?.ClearFocus();
return;
}
Logging.Log(LogType.Warning, $"Failed to Hide Keyboard via {nameof(inputMethodManager.ToggleSoftInputFromWindow)}...");
}
public void ReInitializeInputMethod()
{
inputMethodManager = InputMethodManager.FromContext((Context) mainActivity);
}
没有一个空检查返回真,即没有什么是空的。 HideKeyboard 方法中名为 success 的变量在 99% 的情况下在 android 版本 9 上调用时返回 false。在 1% 为 true 的情况下,键盘仍处于打开状态。如果键盘仍然显示在 HideKeyboard 的末尾,则代码尝试通过在 HideKeyboardAttemptTwo 方法中切换来关闭键盘。在 Android 9 上执行这两种方法中的任何一种都行不通,但是在 Android 7.1 上运行完全相同的代码就可以了。
我不完全确定我是否正确实现了 ToggleSoftInputFromWindow 的使用,它只能在键盘打开时运行,即始终用于隐藏键盘。
重申我的问题:如何在 Android 9 上成功隐藏键盘。
如果需要任何其他信息,请询问,我会尝试查找并提供。
【问题讨论】:
标签: c# android xamarin.forms xamarin.android android-softkeyboard