Esempio n. 1
0
        void HandleEditingChanged(object sender, TextChangedEventArgs e)
        {
            if (_internalTextChange)
            {
                return;
            }

            string newText = _textBox.Text;

            if (_uppercase)
            {
                newText = newText.ToUpper();

                int selStart  = _textBox.SelectionStart;
                int selLength = _textBox.SelectionLength;

                _textBox.Text = newText;
                _textBox.Select(selStart, selLength);
            }

            string text = newText;

            if (_inputType.HasFlag(TextInputType.Digits))
            {
                text = text.Replace(",", "").Replace(".", "");
            }

            text = _controller.TextChanged(text);

            if (text != newText)
            {
                SetText(text);
            }
        }
        InputTypes TypeFromContext(TextInputType context)
        {
            InputTypes value = Android.Text.InputTypes.TextFlagNoSuggestions | Android.Text.InputTypes.ClassText;

            switch (context & TextInputType.TypeFilter)
            {
            case TextInputType.Email:
                value = InputTypes.TextVariationEmailAddress | Android.Text.InputTypes.ClassText;
                break;

            case TextInputType.FirstLetterUppercase:
                value = InputTypes.TextFlagCapWords | Android.Text.InputTypes.ClassText;
                break;

            case TextInputType.Digits:
                value = InputTypes.ClassNumber | InputTypes.NumberFlagSigned;
                break;

            case TextInputType.Numeric:
                value = InputTypes.NumberFlagDecimal | InputTypes.ClassNumber;
                break;

            case TextInputType.NormalText:
                value = InputTypes.TextVariationNormal | Android.Text.InputTypes.ClassText;
                break;

            case TextInputType.Uppercase:
                value = InputTypes.TextFlagCapCharacters | InputTypes.ClassText;
                break;

            case TextInputType.PasswordClass:
                value = InputTypes.TextVariationPassword | Android.Text.InputTypes.ClassText;
                break;

            case TextInputType.MultilineText:
                value = InputTypes.TextFlagMultiLine | Android.Text.InputTypes.TextFlagImeMultiLine;
                break;
            }

            if (context.HasFlag(TextInputType.NoSuggestions))
            {
                value |= InputTypes.TextFlagNoSuggestions | InputTypes.TextVariationVisiblePassword;
            }

            return(value);
        }
Esempio n. 3
0
        public NativeInput(Rectangle position, TextInputType keyboardContext, string text, int textSize, Align align, ITextEdit controller)
        {
            if (CurrentFocus != null)
            {
                CurrentFocus.Unfocus();
            }

            _inputType = keyboardContext;

            _controller  = controller;
            CurrentFocus = this;

            text = text.Replace('\n', '\r');

            var    info  = DisplayInformation.GetForCurrentView();
            double scale = info.RawPixelsPerViewPixel;

            double fontSize = textSize;

            if (keyboardContext == TextInputType.Password)
            {
                _usePasswordBox = true;
                _passwordBox.PasswordChanged += HandleEditingChangedPassword;
                _passwordBox.LostFocus       += HandleEditingDidEnd;
                _passwordBox.KeyUp           += HandleKeyUp;

                Canvas.SetLeft(_passwordBox, (position.X + 2) / scale);

                _passwordBox.Width  = (position.Width - 4) / scale;
                _passwordBox.Height = _passwordBox.MinHeight;
                Canvas.SetTop(_passwordBox, (position.Center.Y / scale - _passwordBox.Height / 2));

                _passwordBox.FontSize = fontSize;

                _passwordBox.Visibility = Visibility.Visible;
                _passwordBox.Focus(FocusState.Programmatic);

                SetText(text);
            }
            else
            {
                _usePasswordBox = false;

                _textBox.TextChanged += HandleEditingChanged;
                _textBox.LostFocus   += HandleEditingDidEnd;
                _textBox.KeyUp       += HandleKeyUp;

                Canvas.SetLeft(_textBox, (position.X + 2) / scale);


                _textBox.Width = (position.Width - 2) / scale;

                switch (align & Align.Horz)
                {
                case Align.Left:
                    _textBox.TextAlignment = TextAlignment.Left;
                    break;

                case Align.Right:
                    _textBox.TextAlignment = TextAlignment.Right;
                    break;

                case Align.Center:
                    _textBox.TextAlignment = TextAlignment.Center;
                    break;
                }

                _inputScope.Names.Clear();
                _inputScope.Names.Add(NameFromContext(keyboardContext));
                _textBox.InputScope = _inputScope;

                _textBox.IsTextPredictionEnabled = !keyboardContext.HasFlag(TextInputType.NoSuggestions);
                _uppercase = keyboardContext.HasFlag(TextInputType.Uppercase);

                if (keyboardContext == TextInputType.MultilineText)
                {
                    _textBox.VerticalContentAlignment = VerticalAlignment.Top;
                    _textBox.AcceptsReturn            = true;
                    _textBox.TextWrapping             = TextWrapping.Wrap;
                    _multiline = true;

                    Canvas.SetTop(_textBox, (position.Y + 2) / scale);
                    _textBox.Height = (position.Height - 4) / scale;
                }
                else
                {
                    _textBox.VerticalContentAlignment = VerticalAlignment.Center;
                    _textBox.AcceptsReturn            = false;
                    _textBox.TextWrapping             = keyboardContext.HasFlag(TextInputType.Wrap) ? TextWrapping.Wrap : TextWrapping.NoWrap;

                    _multiline = false;

                    _textBox.Height = _textBox.MinHeight;
                    Canvas.SetTop(_textBox, (position.Center.Y / scale - _textBox.Height / 2));
                }

                SetText(text);

                CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;

                _textBox.FontSize   = fontSize;
                _textBox.Visibility = Visibility.Visible;
                _textBox.Focus(FocusState.Programmatic);
            }
        }
Esempio n. 4
0
        public NativeInput(Rectangle position, TextInputType keyboardContext, string text, int textSize, Align align, ITextEdit controller)
        {
            if (CurrentFocus != null)
            {
                CurrentFocus.Unfocus();
            }

            _controller  = controller;
            CurrentFocus = this;

            if (_textField == null)
            {
                InitTextField();
            }

            text = text.Replace('\n', '\r');

            Bottom      = position.Bottom + (int)Platform.PointsToPixels(10);
            position.Y += AppMain.Current.SetFocus(this);

            float x      = Platform.PixelsToPoints(position.X) + 1;
            float y      = Platform.PixelsToPoints(position.Y) + 2;
            float width  = Platform.PixelsToPoints(position.Width) - 2;
            float height = Platform.PixelsToPoints(position.Height) - 4;

            if (keyboardContext.HasFlag(TextInputType.PasswordClass))
            {
                keyboardContext &= ~(TextInputType.NoSuggestions);
            }

            if (keyboardContext.HasFlag(TextInputType.MultilineText))
            {
                _useTextView = true;

                _textView.Frame                  = new System.Drawing.RectangleF(x, y, width, height);
                _textView.BackgroundColor        = new UIColor(255, 255, 255, 255);
                _textView.TextColor              = new UIColor(0, 0, 0, 255);
                _textView.Opaque                 = true;
                _textView.KeyboardType           = TypeFromContext(keyboardContext);
                _textView.AutocapitalizationType = AutoCapitalizationFromContext(keyboardContext);

                _textView.KeyboardType = UIKeyboardType.Default;

                _textView.AutocorrectionType = keyboardContext.HasFlag(TextInputType.NoSuggestions) ?  UITextAutocorrectionType.No :  UITextAutocorrectionType.Default;

                _textView.SecureTextEntry = keyboardContext.HasFlag(TextInputType.PasswordClass);

                _textView.Font = UIFont.FromName("Helvetica", textSize);
                SetText(text);

                _textView.Ended += HandleEnded;
                _textView.BecomeFirstResponder();
            }
            else
            {
                _textField.Frame                  = new System.Drawing.RectangleF(x, y, width, height);
                _textField.BackgroundColor        = new UIColor(255, 255, 255, 255);
                _textField.TextColor              = new UIColor(0, 0, 0, 255);
                _textField.Opaque                 = true;
                _textField.KeyboardType           = TypeFromContext(keyboardContext);
                _textField.AutocapitalizationType = AutoCapitalizationFromContext(keyboardContext);

                _textField.ResignFirstResponder();

                _textField.AutocorrectionType = keyboardContext.HasFlag(TextInputType.NoSuggestions) ?  UITextAutocorrectionType.No :  UITextAutocorrectionType.Default;
                _textField.SecureTextEntry    = keyboardContext.HasFlag(TextInputType.PasswordClass);

                _textField.ClearsOnBeginEditing = false;

                _textField.Font = UIFont.FromName("Helvetica", textSize);

                switch (align & Align.Horz)
                {
                case Align.Center:
                    _textField.TextAlignment = UITextAlignment.Center;
                    break;

                case Align.Left:
                    _textField.TextAlignment = UITextAlignment.Left;
                    break;

                case Align.Right:
                    _textField.TextAlignment = UITextAlignment.Right;
                    break;
                }

                SetText(text);
                _textField.EditingChanged += HandleEditingChanged;
                _textField.EditingDidEnd  += HandleEditingDidEnd;


                _textField.ShouldReturn = delegate
                {
                    if ((keyboardContext & TextInputType.TypeFilter) != TextInputType.MultilineText)
                    {
                        _controller.Return();
                        return(true);
                    }

                    return(false);
                };

                _textField.BecomeFirstResponder();
            }
        }
        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);
        }