开发者

programming style with modal dialog

in my android application at some event in an activity I want to ask the user for a name (string). I know how to do this: call showDialog, create the dialog in the Activity.onCreateDialog method (I need to suppl开发者_如何学运维y a string for the label) and handle the result in the onClick of the dialog. This works fine and to my satisfaction.

BUT this way I have three different places, where this simple task spreads throughout the code of my activity. I would much more prefer to keep this code together, to write some code like this:

string result;
if (showSimpleEditDialog(idForLabelString, result)==DIALOG_OK)
{
    // do something with the result
}

or maybe with a class instance

SimpleEditDialog dlg = new SimpleEditDialog(idForLabelString);
if (dlg.showModal()==DIALOG_OK)
{
    string result = dgl.getResult();
    // do something with the result
}

(The "idForLabelString" would be some resource id for the label to use, DIALOG_OK would be some constant returned when the user clicks OK) I know, I would have to write this methodes or this class. But for better readibility of my code I would do it. Any suggestions?

Thank you,

Gerhard


"BUT this way I have three different places, where this simple task spreads throughout the code"

So why don't you create a Method for this task? What you are talking about sounds like some sort of 'ActionListener' to me. This can be done in Java/Swing, but not in Android.

But, if you have three Dialogs, which all need to do the same when "YES" e.g. "NO" is pressed, you could define the 'DialogInterface.OnClickListener()' as a global inner-Class (or in a second class which extends the 'onClickListener') and then use it for all the Dialogs.


Now actually the problem with modal dialogs is mostly a problem with programm flow. You want to keep things together that belong together. You want to display a dialog that returns "ok" or "cancel" and additionaly e.g. a string that the user entered into one of the dialog widgets.

I do not want to write half of the code up to the line where I need the result of the dialog on one place and the rest of the code on another place namely the onClickListener of the dialog.

In some scenarios the first dialog might invoke a second dialog e.g. to specify a color which is not in the list of the first dialog's ListView.

Your code will be scattered all over the place (in each dialog's button onClickListener) and will be hard to read or to maintain.

Now after having written some unclear code like that I came up with the following solution which certainly respects the android design guides.

Instead of directly showing a dialog I create a Handler derived class which handles messages.

I send it a first message which creates and shows a dialog. It also forwards the handler to the dialog and the diaolg in it's onStop method sends another message to the handler, indicating the end of the dialog. There you can examine the dialogs properties, the contents of the edit fields or whether it was stopped with OK or CANCEL. Now in the message handler all the logic of the task sits in different cases of the messages arg1 value.

Some cases might be skipped (e.g. the user selected a standard color and did not need a special color dialog).

The dialogs are independant of the scenario from which they are called and in their code only reflect their simple task (selecting from a list, some checkboxes etc.). They may be reused from other scenarios.

Following a kind of a template how to use this approach:

public class DoSomethingWithDialogs extends Handler
{
Context context; // from which it was called
final static int stepBegin = 0;
final static int stepNext = 1;
final static int stepSomethingElse = 2;
final static int stepLast = 3;

protected DoSomethingWithDialogs(Context context)
{   
    this.context = context;
}

public static void start(Context context)
{   // this is the main (only) entry point from outside
    DoSomethingWithDialogs st = new DoSomethingWithDialogs(context);
    st.sendMessage(st.obtainMessage(0, stepBegin, 0));
}

@Override
public void handleMessage(Message msg)
{
    // step by step handling the task
    switch (msg.arg1)
    {
    case stepBegin:
    {
        SomeDlg somedlg = new SomeDlg(context, this, stepNext);
       // when the dialog closes, it sends a message to this with stepNext as arg1
        somedlg.show();
    }
        break;
    case stepNext:
    {   // this message was send by the dialog when it finished
        SomeDlg somedlg = (SomeDlg) msg.obj;
        if (msg.arg2 == Dialog.BUTTON_NEGATIVE)
        {
            // has been canceled, nothing to do
        } else
        {
            if (somedlg.someProperty)
            {

            } else
            {
                sendMessage(obtainMessage(0, stepSomethingElse, 0));
            }
        }
    }
        break;
    case stepSomethingElse:
        break;
    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜