AndroidInputMethod.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using Android.Content;
  3. using Android.Runtime;
  4. using Android.Views;
  5. using Android.Views.InputMethods;
  6. using Avalonia.Input;
  7. using Avalonia.Input.TextInput;
  8. namespace Avalonia.Android
  9. {
  10. class AndroidInputMethod<TView> : ITextInputMethodImpl
  11. where TView: View, IInitEditorInfo
  12. {
  13. private readonly TView _host;
  14. private readonly InputMethodManager _imm;
  15. private IInputElement _inputElement;
  16. public AndroidInputMethod(TView host)
  17. {
  18. if (host.OnCheckIsTextEditor() == false)
  19. throw new InvalidOperationException("Host should return true from OnCheckIsTextEditor()");
  20. _host = host;
  21. _imm = host.Context.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>();
  22. _host.Focusable = true;
  23. _host.FocusableInTouchMode = true;
  24. _host.ViewTreeObserver.AddOnGlobalLayoutListener(new SoftKeyboardListener(_host));
  25. }
  26. public void Reset()
  27. {
  28. _imm.RestartInput(_host);
  29. }
  30. public void SetActive(bool active)
  31. {
  32. if (active)
  33. {
  34. _host.RequestFocus();
  35. Reset();
  36. _imm.ShowSoftInput(_host, ShowFlags.Implicit);
  37. }
  38. else
  39. _imm.HideSoftInputFromWindow(_host.WindowToken, HideSoftInputFlags.None);
  40. }
  41. public void SetCursorRect(Rect rect)
  42. {
  43. }
  44. public void SetOptions(TextInputOptionsQueryEventArgs options)
  45. {
  46. if (_inputElement != null)
  47. {
  48. _inputElement.PointerReleased -= RestoreSoftKeyboard;
  49. }
  50. _inputElement = options.Source as InputElement;
  51. if (_inputElement == null)
  52. {
  53. _imm.HideSoftInputFromWindow(_host.WindowToken, HideSoftInputFlags.None);
  54. }
  55. _host.InitEditorInfo((outAttrs) =>
  56. {
  57. outAttrs.InputType = options.ContentType switch
  58. {
  59. TextInputContentType.Email => global::Android.Text.InputTypes.TextVariationEmailAddress,
  60. TextInputContentType.Number => global::Android.Text.InputTypes.ClassNumber,
  61. TextInputContentType.Password => global::Android.Text.InputTypes.TextVariationPassword,
  62. TextInputContentType.Phone => global::Android.Text.InputTypes.ClassPhone,
  63. TextInputContentType.Url => global::Android.Text.InputTypes.TextVariationUri,
  64. _ => global::Android.Text.InputTypes.ClassText
  65. };
  66. if (options.AutoCapitalization)
  67. {
  68. outAttrs.InitialCapsMode = global::Android.Text.CapitalizationMode.Sentences;
  69. outAttrs.InputType |= global::Android.Text.InputTypes.TextFlagCapSentences;
  70. }
  71. if (options.Multiline)
  72. outAttrs.InputType |= global::Android.Text.InputTypes.TextFlagMultiLine;
  73. outAttrs.ImeOptions |= ImeFlags.NoFullscreen | ImeFlags.NoExtractUi;
  74. });
  75. //_inputElement.PointerReleased += RestoreSoftKeyboard;
  76. }
  77. private void RestoreSoftKeyboard(object sender, PointerReleasedEventArgs e)
  78. {
  79. _imm.ShowSoftInput(_host, ShowFlags.Implicit);
  80. }
  81. }
  82. }