4 #if SILICONSTUDIO_PLATFORM_ANDROID
8 using Android.Views.InputMethods;
10 using Android.Text.Method;
11 using SiliconStudio.Core;
14 namespace SiliconStudio.
Paradox.UI.Controls
16 public partial class EditText
18 private static MyAndroidEditText staticEditText;
19 private static EditText activeEditText;
21 private MyAndroidEditText editText;
23 private InputMethodManager inputMethodManager;
25 private class MyAndroidEditText :
Android.Widget.EditText
27 public MyAndroidEditText(Context context)
32 public override bool OnKeyPreIme(Keycode keyCode, KeyEvent e)
34 if (e.KeyCode == Keycode.Back)
36 if (activeEditText != null)
37 activeEditText.IsSelectionActive =
false;
42 return base.OnKeyPreIme(keyCode, e);
45 protected override void OnDetachedFromWindow()
47 base.OnDetachedFromWindow();
50 staticEditText = null;
54 private static void InitializeStaticImpl()
58 protected override void OnUISystemChanged(UISystem system)
60 base.OnUISystemChanged(system);
63 if (staticEditText == null)
66 staticEditText =
new MyAndroidEditText(PlatformAndroid.Context);
68 var editLayoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
69 editLayoutParams.SetMargins(75, 200, 75, 0);
70 game.Context.EditTextLayout.AddView(staticEditText, editLayoutParams);
74 private void InitializeImpl()
79 private void AndroidEditTextOnAfterTextChanged(
object sender, AfterTextChangedEventArgs afterTextChangedEventArgs)
84 var newText = editText.Text;
85 var oldStart = selectionStart;
86 var newStart = SelectionStart;
88 if (newStart <= oldStart)
90 SetTextInternal(newText,
false);
96 var predicate = CharacterFilterPredicate;
97 for (
int i = oldStart; i < newStart; i++)
99 var character = newText[i];
100 if (predicate == null || predicate(character))
101 builder.Append(character);
104 SetTextInternal(newText.Substring(0, oldStart) + builder + newText.Substring(newStart),
false);
105 newStart = Math.Min(oldStart + builder.Length, text.Length);
108 UpdateTextToEditImpl();
112 private void AndroidEditTextOnEditorAction(
object sender, TextView.EditorActionEventArgs editorActionEventArgs)
114 if (editorActionEventArgs.ActionId == ImeAction.Done)
115 IsSelectionActive =
false;
118 private int GetLineCountImpl()
120 if (editText == null)
123 return editText.LineCount;
126 private void OnMaxLinesChangedImpl()
128 if (editText == null)
131 editText.SetMaxLines(MaxLines);
134 private void OnMinLinesChangedImpl()
136 if (editText == null)
139 editText.SetMinLines(MinLines);
142 private void ActivateEditTextImpl()
144 if(activeEditText != null)
145 throw new Exception(
"Internal error: Can not activate edit text, another edit text is already active");
147 activeEditText =
this;
148 editText = staticEditText;
151 UpdateInputTypeImpl();
153 editText.SetMaxLines(MaxLines);
154 editText.SetMinLines(MinLines);
156 UpdateTextToEditImpl();
157 UpdateSelectionToEditImpl();
160 editText.EditorAction += AndroidEditTextOnEditorAction;
161 editText.AfterTextChanged += AndroidEditTextOnAfterTextChanged;
164 game.Context.EditTextLayout.Visibility = ViewStates.Visible;
167 editText.RequestFocus();
170 inputMethodManager = (InputMethodManager)PlatformAndroid.Context.GetSystemService(Context.InputMethodService);
171 inputMethodManager.ShowSoftInput(staticEditText, ShowFlags.Forced);
174 private void DeactivateEditTextImpl()
176 if (activeEditText == null)
177 throw new Exception(
"Internal error: Can not deactivate the EditText, it is already nullified");
180 editText.EditorAction -= AndroidEditTextOnEditorAction;
181 editText.AfterTextChanged -= AndroidEditTextOnAfterTextChanged;
183 editText.ClearFocus();
186 activeEditText = null;
189 game.Context.EditTextLayout.Visibility = ViewStates.Gone;
192 if (staticEditText != null)
193 inputMethodManager.HideSoftInputFromWindow(staticEditText.WindowToken, HideSoftInputFlags.None);
194 inputMethodManager = null;
196 FocusedElement = null;
199 private static void OnTouchMoveImpl(TouchEventArgs args)
203 private static void OnTouchDownImpl(TouchEventArgs args)
207 private void UpdateInputTypeImpl()
209 if (editText == null)
214 editText.TransformationMethod =
new PasswordTransformationMethod();
215 editText.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword;
219 editText.TransformationMethod = null;
220 editText.InputType = InputTypes.ClassText | InputTypes.TextVariationNormal;
224 private void UpdateTextToEditImpl()
229 if(editText.Text != Text)
230 editText.Text = text;
233 private void UpdateSelectionFromEditImpl()
235 if (editText == null)
238 selectionStart = editText.SelectionStart;
239 selectionStop = editText.SelectionEnd;
242 private void UpdateSelectionToEditImpl()
244 if (editText == null)
247 editText.SetSelection(selectionStart, selectionStop);