EditTextEx CreateEdit(ViewGroup.LayoutParams lp) { var edit = new EditTextEx(AppMain.Activity); edit.LayoutParameters = lp; AppMain.Current.RootView.AddView(edit); return(edit); }
private void InitTextField() { _layoutParams = new RelativeLayout.LayoutParams(10, 10); _layoutParams.SetMargins(0, 0, 0, 0); _textField = CreateEdit(_layoutParams); var drawable = new ShapeDrawable(new RectShape()); drawable.Paint.StrokeWidth = 0; drawable.Paint.SetStyle(Android.Graphics.Paint.Style.Fill); drawable.Paint.Color = new Android.Graphics.Color(255, 255, 255); _textField.SetBackgroundDrawable(drawable); _textField.SetTextColor(Android.Graphics.Color.Black); _textField.SetIncludeFontPadding(false); _textField.Visibility = Android.Views.ViewStates.Invisible; _textField.SetHighlightColor(new Android.Graphics.Color(128, 128, 128)); _textField.SetHintTextColor(new Android.Graphics.Color(128, 128, 128)); _textField.OnBackPressed += UnfocusByBack; }
public NativeInput(Rectangle position, TextInputType textInputType, string text, int textSize, Align textAlign, ITextEdit controller) { if (CurrentFocus != null) { CurrentFocus.Unfocus(); } _controller = controller; CurrentFocus = this; if (_textField != null) { EditTextEx field = _textField; field.OnBackPressed -= UnfocusByBack; _textField = null; DelayedActionInvoker.Instance.AddAction(10, (s) => { AppMain.Current.RootView.RemoveView(field); }); } { InitTextField(); } DisplayMetrics metrics = AppMain.Activity.Resources.DisplayMetrics; int padding = 0; if (textInputType == TextInputType.MultilineText) { padding = 0; } _textField.SetTextSize(Android.Util.ComplexUnitType.Px, (float)(UiUnit.FontUnit * textSize)); _textField.SetHeight(position.Height); _textField.SetPadding(0, padding, _textField.PaddingRight, padding); _textField.InputType = TypeFromContext(textInputType); if (textInputType.HasFlag(TextInputType.Uppercase)) { _textField.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(controller.MaxLength), new InputFilterAllCaps() }); } else { _textField.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(controller.MaxLength) }); } switch (textAlign & Align.Horz) { case Align.Left: _textField.Gravity = GravityFlags.CenterVertical | GravityFlags.Left; break; case Align.Center: _textField.Gravity = GravityFlags.CenterVertical | GravityFlags.CenterHorizontal; break; case Align.Right: _textField.Gravity = GravityFlags.CenterVertical | GravityFlags.Right; break; } if ((textInputType & TextInputType.TypeFilter) == TextInputType.MultilineText) { _textField.SetMaxLines(controller.MaxLines); _textField.EditorAction -= HandleEditorAction; _textField.Gravity = GravityFlags.Left | GravityFlags.Top; _textField.SetSingleLine(false); _textField.ImeOptions = ImeAction.ImeNull | (ImeAction)ImeFlags.NoExtractUi; } else { _textField.SetMaxLines(1); _textField.EditorAction += HandleEditorAction; _textField.SetSingleLine(true); _textField.ImeOptions = (controller.WaitsForReturn ? ImeAction.Next : ImeAction.Done) | (ImeAction)ImeFlags.NoExtractUi; } if (textInputType.HasFlag(TextInputType.NoSuggestions)) { _textField.ImeOptions |= (ImeAction)InputTypes.TextFlagNoSuggestions; } _textField.SetAllCaps((textInputType & TextInputType.TypeFilter) == TextInputType.Uppercase); _textField.TransformationMethod = textInputType == TextInputType.Password ? new PasswordTransformationMethod(): null; _layoutParams.SetMargins(position.X, position.Y + 4, 0, 0); _layoutParams.Width = position.Width; _layoutParams.Height = position.Height - 4; _textField.Text = text; _textField.SetSelection(_textField.Text.Length, _textField.Text.Length); _textField.TextChanged += HandleEditingChanged; _textField.FocusChange += HandleFocusChange; _textField.RequestLayout(); _textField.Visibility = Android.Views.ViewStates.Visible; _textField.SetCursorVisible(true); _textField.RequestFocus(); _textField.RequestFocusFromTouch(); ShowKeyboard(_textField); }