问题描述:

I am wondering how I go about (if it is possible) creating a modal popup in a mono for droid application.

Scenario: The application talks to the customers hosted web server (so this location will be different customer to customer). To use the app the user must specify the connection string of their web server. So when the application starts and it hits the main activity, the first task I do is check if there is a connection string set in the devices application settings. If not I want to throw up a simple modal popup that allows the user to specify a connection to their server.

I dont really want to start a normal activity because the user will be able to click the back button and just go back to the main menu and the app is than in an invalid state because it doesnt know what server to talk to.

Any ideas on how I go about this?

Or should I be structuring the activity chain so that the connection string is entered on the first activity so that if they click back it actually goes out of the app?

Im a little confused.

Thanks in advance

网友答案:

This is possible with AlertDialog. It can create dialogs for simple input with lists, checkboxes, yes/no buttons and custom views.

There is a sample in the Xamarin Sample Repository for different type of dialogs and in the bottom you can find one where a custom view with a username and password field has been added.

So first define your custom view you want to put in the AlertDialog. alert_dialog_connection_entry.xml and is a Layout:

Somewhere in your activity add the code:

var connection_string_view = LayoutInflater.Inflate(Resource.Layout.alert_dialog_connection_entry, null);

var builder = new AlertDialog.Builder(this);
builder.SetTitle("Connection String");
builder.SetView(connection_string_view);
builder.SetPositiveButton("OK", OkClicked);
builder.SetNegativeButton("Cancel", CancelClicked);
builder.Create();
builder.Show();

Add some handlers for the buttons:

private void CancelClicked(object sender, DialogClickEventArgs dialogClickEventArgs)
{
    //Todo
}

private void OkClicked(object sender, DialogClickEventArgs dialogClickEventArgs)
{
    var dialog = sender as AlertDialog;

    if (null != dialog)
    {
        var connectionEdit = dialog.FindViewById(Resource.Id.connectionstring_edit) as EditText;


        if (null != connectionEdit)
            Console.WriteLine("Connection String: {0}", connectionEdit.Text);
    }
}

That should be it. You should be able to put any kind of custom view in the dialog.

网友答案:

If you just want to display a modal popup for letting users put their connection string, you could try this.

First, you need to have a simple layout for how the dialog is presented. In this case, a TextView displaying something like "Connection string:" and an EditText to let the user put connection string is probably enough to go.

Then, you can put this code somewhere in your MainActivity, like after checking application settings or something similar.

        var builder = new AlertDialog.Builder(this);
        var view = LayoutInflater.Inflate(Resource.Layout.ModalDialog, null);
        builder.SetView(view);
        string connectionString = view.FindViewById<EditText>(Resource.Id.ConnectionString).Text;
        AlertDialog alert = builder.Create();
        alert.SetCancelable(false);    //This prevents the dialog from being dismissed by either hit back button or hit out side of the dialog
        alert.SetButton("OK", (s,e)=> ToDo(connectionString));    //Now you have the connection string, to do whatever you want.
        alert.Show();

As you said, the alternative could be allowing user specify the connection string in the first screen. This is a good approach too. I assume you know how to do it, so I didn't post code here.

相关阅读:
Top