public Dialog BuildForm(AppCompatActivity activity, PromptFormConfig config) { var layout = new LinearLayout(activity) { Orientation = Orientation.Vertical, }; var txts = new Dictionary <string, EditText>(); var result = new Dictionary <string, string>(); foreach (var item in config.Items) { var txt = new EditText(activity) { Hint = item.Placeholder, }; if (item.Text != null) { txt.Text = item.Text; txt.SetSelection(item.Text.Length); } if (item.MaxLength != null) { txt.SetFilters(new[] { new InputFilterLengthFilter(item.MaxLength.Value) }); } SetInputType(txt, item.InputType); layout.AddView(txt, ViewGroup.LayoutParams.MatchParent); txts[item.Key] = (txt); result[item.Key] = string.Empty; } Action action = () => { foreach (var item in txts) { result[item.Key] = item.Value.Text.Trim(); } }; var builder = new AppCompatAlertDialog.Builder(activity, config.AndroidStyleId ?? 0) .SetCancelable(false) .SetMessage(config.Message) .SetTitle(config.Title) .SetView(layout) .SetPositiveButton(config.OkText, (s, a) => { action(); config.OnAction?.Invoke(new PromptFormResult(true, result)); }); if (config.IsCancellable) { builder.SetNegativeButton(config.CancelText, (s, a) => { action(); config.OnAction?.Invoke(new PromptFormResult(false, result)); }); } var dialog = builder.Create(); return(dialog); }
public static UIAlertController Build(PromptFormConfig config) { var controller = UIAlertController.Create(config.Title, config.Message, UIAlertControllerStyle.Alert); var result = new Dictionary <string, string>(); var inputs = new Dictionary <string, UITextField>(); foreach (var item in config.Items) { var txt = new UITextField(); controller.AddTextField((f) => { txt = f; SetInputType(txt, item.InputType); txt.Placeholder = item.Placeholder ?? string.Empty; txt.Text = item.Text ?? string.Empty; if (item.MaxLength != null) { txt.ShouldChangeCharacters = (field, replacePosition, replacement) => { var updatedText = new StringBuilder(field.Text); updatedText.Remove((int)replacePosition.Location, (int)replacePosition.Length); updatedText.Insert((int)replacePosition.Location, replacement); return(updatedText.ToString().Length <= item.MaxLength.Value); }; } }); inputs[item.Key] = txt; } Action action = () => { foreach (var item in inputs) { result[item.Key] = item.Value.Text.Trim(); } }; var okButton = UIAlertAction.Create(config.OkText, UIAlertActionStyle.Default, (_) => { action(); config.OnAction?.Invoke(new PromptFormResult(true, result)); }); controller.AddAction(okButton); if (config.IsCancellable) { controller.AddAction(UIAlertAction.Create(config.CancelText, UIAlertActionStyle.Cancel, (_) => { action(); config.OnAction?.Invoke(new PromptFormResult(false, result)); })); } return(controller); }