Skip to content

Commit

Permalink
Hide Keyboard in Android Entry and Editor when back key is pressed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz authored Feb 25, 2022
1 parent 461c203 commit 3551378
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ void UpdateInputType()

abstract protected void UpdatePlaceholderColor();

[PortHandler]
void OnKeyboardBackPressed(object sender, EventArgs eventArgs)
{
Control?.ClearFocus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public FormsEditText(Context context) : base(context)
{
}


[PortHandler]
public override bool OnKeyPreIme(Keycode keyCode, KeyEvent e)
{
if (keyCode != Keycode.Back || e.Action != KeyEventActions.Down)
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Editor/EditorHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class EditorHandler : ViewHandler<IEditor, AppCompatEditText>

protected override AppCompatEditText CreatePlatformView()
{
var editText = new AppCompatEditText(Context)
var editText = new MauiEditText(Context)
{
ImeOptions = ImeAction.Done,
Gravity = GravityFlags.Top,
Expand Down
20 changes: 18 additions & 2 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Android.Content.Res;
using System;
using Android.Content.Res;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Text;
Expand All @@ -19,7 +20,7 @@ public partial class EntryHandler : ViewHandler<IEntry, AppCompatEditText>

protected override AppCompatEditText CreatePlatformView()
{
var nativeEntry = new AppCompatEditText(Context);
var nativeEntry = new MauiEditText(Context);
_defaultPlaceholderColors = nativeEntry.HintTextColors;
return nativeEntry;
}
Expand All @@ -34,6 +35,11 @@ protected override void ConnectHandler(AppCompatEditText platformView)
platformView.FocusChange += OnFocusedChange;
platformView.Touch += OnTouch;
platformView.EditorAction += OnEditorAction;

if (platformView is IMauiEditText mauiEditText)
{
mauiEditText.OnKeyboardBackPressed += OnKeyboardBackPressed;
}
}

protected override void DisconnectHandler(AppCompatEditText platformView)
Expand All @@ -43,6 +49,11 @@ protected override void DisconnectHandler(AppCompatEditText platformView)
platformView.FocusChange -= OnFocusedChange;
platformView.Touch -= OnTouch;
platformView.EditorAction -= OnEditorAction;

if (platformView is IMauiEditText mauiEditText)
{
mauiEditText.OnKeyboardBackPressed -= OnKeyboardBackPressed;
}
}

public static void MapBackground(IEntryHandler handler, IEntry entry) =>
Expand Down Expand Up @@ -132,5 +143,10 @@ void OnEditorAction(object? sender, EditorActionEventArgs e)

e.Handled = true;
}

void OnKeyboardBackPressed(object? sender, EventArgs eventArgs)
{
PlatformView?.ClearFocus();
}
}
}
12 changes: 12 additions & 0 deletions src/Core/src/Platform/Android/IMauiEditText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Android.Views;

namespace Microsoft.Maui.Platform
{
public interface IMauiEditText
{
bool OnKeyPreIme(Keycode keyCode, KeyEvent? e);

event EventHandler OnKeyboardBackPressed;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#nullable disable
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views.InputMethods;
using Android.Widget;
using Microsoft.Maui.Essentials;
using AView = Android.Views.View;

namespace Microsoft.Maui.Controls.Platform
namespace Microsoft.Maui.Platform
{
internal static class KeyboardManager
public static class KeyboardManager
{
internal static void HideKeyboard(this AView inputView, bool overrideValidation = false)
public static void HideKeyboard(this AView inputView, bool overrideValidation = false)
{
if (inputView == null)
throw new ArgumentNullException(nameof(inputView) + " must be set before the keyboard can be hidden.");
Expand All @@ -26,7 +27,7 @@ internal static void HideKeyboard(this AView inputView, bool overrideValidation
}
}

internal static void ShowKeyboard(this TextView inputView)
public static void ShowKeyboard(this TextView inputView)
{
if (inputView == null)
throw new ArgumentNullException(nameof(inputView) + " must be set before the keyboard can be shown.");
Expand All @@ -40,7 +41,7 @@ internal static void ShowKeyboard(this TextView inputView)
}
}

internal static void ShowKeyboard(this SearchView searchView)
public static void ShowKeyboard(this SearchView searchView)
{
if (searchView == null)
{
Expand Down Expand Up @@ -73,7 +74,7 @@ internal static void ShowKeyboard(this SearchView searchView)
}
}

internal static void ShowKeyboard(this AView view)
public static void ShowKeyboard(this AView view)
{
switch (view)
{
Expand All @@ -100,7 +101,7 @@ void ShowKeyboard()
view.ShowKeyboard();
};

Device.BeginInvokeOnMainThread(ShowKeyboard);
MainThread.BeginInvokeOnMainThread(ShowKeyboard);
}
}
}
36 changes: 36 additions & 0 deletions src/Core/src/Platform/Android/MauiEditText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Android.Content;
using Android.Views;
using AndroidX.AppCompat.Widget;

namespace Microsoft.Maui.Platform
{
public class MauiEditText : AppCompatEditText, IMauiEditText
{
event EventHandler? _onKeyboardBackPressed;
event EventHandler? IMauiEditText.OnKeyboardBackPressed
{
add => _onKeyboardBackPressed += value;
remove => _onKeyboardBackPressed -= value;
}

public MauiEditText(Context context) : base(context)
{

}

public override bool OnKeyPreIme(Keycode keyCode, KeyEvent? e)
{
if (keyCode != Keycode.Back || e?.Action != KeyEventActions.Down)
{
return base.OnKeyPreIme(keyCode, e);
}

this.HideKeyboard();

_onKeyboardBackPressed?.Invoke(this, EventArgs.Empty);

return true;
}
}
}

0 comments on commit 3551378

Please sign in to comment.