public new Dialog Build(Activity activity, PromptConfig config)
        {
            var txt = new EditText(activity)
            {
                Id   = Int32.MaxValue,
                Hint = config.Placeholder
            };

            if (config.Text != null)
            {
                txt.Text         = config.Text;
                txt.FocusChange += (o, e) => { if (e.HasFocus)
                                               {
                                                   txt.SelectAll();
                                               }
                };                                                                 // modify
            }

            if (config.MaxLength != null)
            {
                txt.SetFilters(new[] { new InputFilterLengthFilter(config.MaxLength.Value) });
            }

            SetInputType(txt, config.InputType);

            var builder = new AlertDialog.Builder(activity, config.AndroidStyleId ?? 0)
                          .SetCancelable(false)
                          .SetMessage(config.Message)
                          .SetTitle(config.Title)
                          .SetView(txt)
                          .SetPositiveButton(config.OkText, (s, a) =>
                                             config.OnAction(new PromptResult(true, txt.Text))
                                             );

            if (config.IsCancellable)
            {
                builder.SetNegativeButton(config.CancelText, (s, a) =>
                                          config.OnAction(new PromptResult(false, txt.Text))
                                          );
            }
            var dialog = builder.Create();

            this.HookTextChanged(dialog, txt, config);

            return(dialog);
        }
Exemplo n.º 2
0
        public override IDisposable Prompt(PromptConfig config)
        {
            Dispatch(() =>
            {
                var dialog = new FormsContentDialog()
                {
                    DataContext = config,
                    Title       = config.Title,
                    // Content will be set
                    IsPrimaryButtonEnabled   = true,
                    PrimaryButtonText        = config.OkText,
                    IsSecondaryButtonEnabled = config.IsCancellable,
                    SecondaryButtonText      = config.CancelText
                };

                if (config.InputType == Acr.UserDialogs.InputType.Password || config.InputType == Acr.UserDialogs.InputType.NumericPassword)
                {
                    var control = new PasswordPromptControl();
                    control.PasswordEdit.PasswordChanged += (s, e) =>
                    {
                        config.Text = control.PasswordEdit.Password;
                        if (config.OnTextChanged != null)
                        {
                            var args = new PromptTextChangedArgs()
                            {
                                Value = control.PasswordEdit.Password
                            };
                            config.OnTextChanged(args);
                            dialog.IsPrimaryButtonEnabled = args.IsValid;
                            if (control.PasswordEdit.Password != args.Value)
                            {
                                control.PasswordEdit.Password = args.Value;
                            }
                        }
                    };
                    dialog.Content = control;
                    // First run of text changed
                    if (config.OnTextChanged != null)
                    {
                        var args = new PromptTextChangedArgs()
                        {
                            Value = control.PasswordEdit.Password
                        };
                        config.OnTextChanged(args);
                        dialog.IsPrimaryButtonEnabled = args.IsValid;
                        control.PasswordEdit.Password = args.Value;
                    }
                }
                else
                {
                    var control = new DefaultPromptControl();
                    control.TextEdit.TextChanged += (s, e) =>
                    {
                        if (config.OnTextChanged != null)
                        {
                            var args = new PromptTextChangedArgs()
                            {
                                Value = control.TextEdit.Text
                            };
                            config.OnTextChanged(args);
                            dialog.IsPrimaryButtonEnabled = args.IsValid;
                            if (control.TextEdit.Text != args.Value)
                            {
                                int selStart                    = control.TextEdit.SelectionStart;
                                control.TextEdit.Text           = args.Value;
                                control.TextEdit.SelectionStart = selStart;
                            }
                        }
                    };
                    dialog.Content = control;
                    // First run of text changed
                    if (config.OnTextChanged != null)
                    {
                        var args = new PromptTextChangedArgs()
                        {
                            Value = control.TextEdit.Text
                        };
                        config.OnTextChanged(args);
                        dialog.IsPrimaryButtonEnabled = args.IsValid;
                        int selStart                    = control.TextEdit.SelectionStart;
                        control.TextEdit.Text           = args.Value;
                        control.TextEdit.SelectionStart = selStart;
                    }
                }

                dialog.PrimaryButtonClick   += (s, e) => { HideContentDialog(); config.OnAction(new PromptResult(true, config.Text)); e.Cancel = true; };
                dialog.SecondaryButtonClick += (s, e) => { HideContentDialog(); config.OnAction(new PromptResult(false, config.Text)); e.Cancel = true; };
                ShowContentDialog(dialog);
            });
            return(new DisposableAction(HideContentDialog));
        }