This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
KeyboardManager.cs
106 lines (91 loc) · 3.34 KB
/
KeyboardManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views.InputMethods;
using Android.Widget;
using AView = Android.Views.View;
namespace Xamarin.Forms.Platform.Android
{
internal static class KeyboardManager
{
internal 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.");
using (var inputMethodManager = (InputMethodManager)inputView.Context.GetSystemService(Context.InputMethodService))
{
if (!overrideValidation && !(inputView is EditText || inputView is TextView || inputView is SearchView))
throw new ArgumentException("inputView should be of type EditText, SearchView, or TextView");
IBinder windowToken = inputView.WindowToken;
if (windowToken != null && inputMethodManager != null)
inputMethodManager.HideSoftInputFromWindow(windowToken, HideSoftInputFlags.None);
}
}
internal static void ShowKeyboard(this TextView inputView)
{
if (inputView == null)
throw new ArgumentNullException(nameof(inputView) + " must be set before the keyboard can be shown.");
using (var inputMethodManager = (InputMethodManager)inputView.Context.GetSystemService(Context.InputMethodService))
{
// The zero value for the second parameter comes from
// https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#showSoftInput(android.view.View,%20int)
// Apparently there's no named value for zero in this case
inputMethodManager?.ShowSoftInput(inputView, 0);
}
}
internal static void ShowKeyboard(this SearchView searchView)
{
if (searchView == null)
{
throw new ArgumentNullException(nameof(searchView));
}
// Dig into the SearchView and find the actual TextView that we want to show keyboard input for
int searchViewTextViewId = searchView.Resources.GetIdentifier("android:id/search_src_text", null, null);
if (searchViewTextViewId == 0)
{
// Cannot find the resource Id; nothing else to do
return;
}
var textView = searchView.FindViewById(searchViewTextViewId);
if (textView == null)
{
// Cannot find the TextView; nothing else to do
return;
}
using (var inputMethodManager = (InputMethodManager)searchView.Context.GetSystemService(Context.InputMethodService))
{
// The zero value for the second parameter comes from
// https://developer.android.com/reference/android/view/inputmethod/InputMethodManager#showSoftInput(android.view.View,%20int)
// Apparently there's no named value for zero in this case
inputMethodManager?.ShowSoftInput(textView, 0);
}
}
internal static void ShowKeyboard(this AView view)
{
switch (view)
{
case SearchView searchView:
searchView.ShowKeyboard();
break;
case TextView textView:
textView.ShowKeyboard();
break;
}
}
internal static void PostShowKeyboard(this AView view)
{
void ShowKeyboard()
{
// Since we're posting this on the queue, it's possible something else will have disposed of the view
// by the time the looper is running this, so we have to verify that the view is still usable
if (view.IsDisposed())
{
return;
}
view.ShowKeyboard();
};
Device.BeginInvokeOnMainThread(ShowKeyboard);
}
}
}