Exemplo n.º 1
0
        internal static void SetInputType(this EditText editText, ITextInput textInput)
        {
            if (textInput.IsReadOnly)
            {
                editText.InputType = InputTypes.Null;
            }
            else
            {
                var keyboard = textInput.Keyboard;
                var nativeInputTypeToUpdate = keyboard.ToInputType();

                if (keyboard is not CustomKeyboard)
                {
                    // TODO: IsSpellCheckEnabled handling must be here.

                    if ((nativeInputTypeToUpdate & InputTypes.TextFlagNoSuggestions) != InputTypes.TextFlagNoSuggestions)
                    {
                        if (!textInput.IsTextPredictionEnabled)
                        {
                            nativeInputTypeToUpdate |= InputTypes.TextFlagNoSuggestions;
                        }
                    }
                }

                if (keyboard == Keyboard.Numeric)
                {
                    editText.KeyListener = LocalizedDigitsKeyListener.Create(editText.InputType);
                }

                if (textInput is IEntry entry && entry.IsPassword)
                {
                    if ((nativeInputTypeToUpdate & InputTypes.ClassText) == InputTypes.ClassText)
                    {
                        nativeInputTypeToUpdate |= InputTypes.TextVariationPassword;
                    }

                    if ((nativeInputTypeToUpdate & InputTypes.ClassNumber) == InputTypes.ClassNumber)
                    {
                        nativeInputTypeToUpdate |= InputTypes.NumberVariationPassword;
                    }
                }

                editText.InputType = nativeInputTypeToUpdate;
            }

            if (textInput is IEditor)
            {
                editText.InputType |= InputTypes.TextFlagMultiLine;
            }
        }
Exemplo n.º 2
0
        internal static void SetInputType(this EditText editText, ITextInput textInput)
        {
            var previousCursorPosition = editText.SelectionStart;
            var keyboard = textInput.Keyboard;
            var nativeInputTypeToUpdate = keyboard.ToInputType();

            if (keyboard is not CustomKeyboard)
            {
                // TODO: IsSpellCheckEnabled handling must be here.

                if ((nativeInputTypeToUpdate & InputTypes.TextFlagNoSuggestions) != InputTypes.TextFlagNoSuggestions)
                {
                    if (!textInput.IsTextPredictionEnabled)
                    {
                        nativeInputTypeToUpdate |= InputTypes.TextFlagNoSuggestions;
                    }
                }
            }

            if (keyboard == Keyboard.Numeric)
            {
                editText.KeyListener = LocalizedDigitsKeyListener.Create(editText.InputType);
            }

            bool hasPassword = false;

            if (textInput is IEntry entry && entry.IsPassword)
            {
                if ((nativeInputTypeToUpdate & InputTypes.ClassText) == InputTypes.ClassText)
                {
                    nativeInputTypeToUpdate |= InputTypes.TextVariationPassword;
                }

                if ((nativeInputTypeToUpdate & InputTypes.ClassNumber) == InputTypes.ClassNumber)
                {
                    nativeInputTypeToUpdate |= InputTypes.NumberVariationPassword;
                }

                hasPassword = true;
            }

            editText.InputType = nativeInputTypeToUpdate;

            if (textInput is IEditor)
            {
                editText.InputType |= InputTypes.TextFlagMultiLine;
            }

            if (hasPassword && textInput is IElement element)
            {
                var services = element.Handler?.MauiContext?.Services;

                if (services == null)
                {
                    return;
                }

                var fontManager = services.GetRequiredService <IFontManager>();
                editText.UpdateFont(textInput, fontManager);
            }

            // If we implement the OnSelectionChanged method, this method is called after a keyboard layout change with SelectionStart = 0,
            // Let's restore the cursor position to its previous location.
            editText.SetSelection(previousCursorPosition);
        }