Guest User

Untitled

a guest
Dec 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. [assembly: ExportRenderer(typeof(MyKeyboardPage), typeof(KeyboardPageRenderer))]
  2. ...
  3. public class KeyboardPageRenderer : PageRenderer
  4. {
  5.  
  6. public CustomKeyboardView mKeyboardView;
  7. public EditText mTargetView;
  8. public Android.InputMethodServices.Keyboard mKeyboard;
  9. Activity activity;
  10. global::Android.Views.View view;
  11.  
  12. protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
  13. {
  14. base.OnElementChanged(e);
  15.  
  16. if (e.OldElement != null || Element == null)
  17. {
  18. return;
  19. }
  20.  
  21. try
  22. {
  23. SetupUserInterface();
  24. SetupEventHandlers();
  25. this.AddView(view);
  26. }
  27. catch (System.Exception ex)
  28. {
  29. System.Diagnostics.Debug.WriteLine(@" ERROR: ", ex.Message);
  30. }
  31. }
  32.  
  33. void SetupUserInterface()
  34. {
  35. activity = this.Context as Activity;
  36. view = activity.LayoutInflater.Inflate(Resource.Layout.activity_keyboard, this, false);
  37.  
  38. mKeyboard = new Android.InputMethodServices.Keyboard(Context, Resource.Xml.keyboard);
  39. mTargetView = view.FindViewById<EditText>(Resource.Id.target);
  40.  
  41. mKeyboardView = view.FindViewById<CustomKeyboardView>(Resource.Id.keyboard_view);
  42. mKeyboardView.Keyboard = mKeyboard;
  43. }
  44.  
  45. void SetupEventHandlers()
  46. {
  47. mTargetView.Touch += (sender, e) =>
  48. {
  49. ShowKeyboardWithAnimation();
  50. e.Handled = false;
  51. mTargetView.ShowSoftInputOnFocus = false;
  52. };
  53.  
  54. mKeyboardView.Key += async (sender, e) =>
  55. {
  56. long eventTime = JavaSystem.CurrentTimeMillis();
  57. KeyEvent ev = new KeyEvent(eventTime, eventTime, KeyEventActions.Down, e.PrimaryCode, 0, 0, 0, 0, KeyEventFlags.SoftKeyboard | KeyEventFlags.KeepTouchMode);
  58.  
  59. DispatchKeyEvent(ev);
  60.  
  61. await Task.Delay(1);
  62.  
  63. mTargetView.RequestFocus();
  64. };
  65. }
  66.  
  67.  
  68. public void ShowKeyboardWithAnimation()
  69. {
  70. if (mKeyboardView.Visibility == ViewStates.Gone)
  71. {
  72. mKeyboardView.Visibility = ViewStates.Visible;
  73. Android.Views.Animations.Animation animation = AnimationUtils.LoadAnimation(
  74. Context,
  75. Resource.Animation.slide_in_bottom
  76. );
  77. mKeyboardView.ShowWithAnimation(animation);
  78. }
  79. }
  80.  
  81. protected override void OnLayout(bool changed, int l, int t, int r, int b)
  82. {
  83. base.OnLayout(changed, l, t, r, b);
  84.  
  85. var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
  86. var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
  87.  
  88. view.Measure(msw, msh);
  89. view.Layout(0, 0, r - l, b - t);
  90. }
  91. }
Add Comment
Please, Sign In to add comment