模式对话框的编程风格
在我的android应用程序中,在某个活动中的某个事件中,我想询问用户一个名称(字符串)。我知道如何执行此操作:调用 showDialog,在 Activity.onCreateDialog 方法中创建对话框(我需要为标签提供字符串)并在对话框的 onClick 中处理结果。这工作正常并且令我满意。
但这样我就有了三个不同的地方,这个简单的任务分布在我的活动的代码中。我更喜欢将这些代码放在一起,编写一些像这样的代码:
string result;
if (showSimpleEditDialog(idForLabelString, result)==DIALOG_OK)
{
// do something with the result
}
或者可能使用类实例
SimpleEditDialog dlg = new SimpleEditDialog(idForLabelString);
if (dlg.showModal()==DIALOG_OK)
{
string result = dgl.getResult();
// do something with the result
}
(“idForLabelString”将是标签使用的一些资源ID,DIALOG_OK将是用户单击时返回的一些常量好的) 我知道,我必须编写这个方法或这个类。但为了我的代码具有更好的可读性,我会这样做。有什么建议吗?
谢谢你,
格哈德
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 supply 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“但是这样我就有了三个不同的地方,这个简单的任务分布在整个代码中”
那么为什么不为这个任务创建一个方法呢?对我来说,你所说的听起来像是某种“ActionListener”。这可以在 Java/Swing 中完成,但不能在 Android 中完成。
但是,如果您有三个对话框,当按下“YES”(例如“NO”)时,它们都需要执行相同的操作,您可以将“DialogInterface.OnClickListener()”定义为全局内部类(或在第二个类中,其中扩展“onClickListener”),然后将其用于所有对话框。
"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.
现在实际上模式对话框的问题主要是程序流程的问题。您希望将属于在一起的事物放在一起。您想要显示一个返回“确定”或“取消”的对话框,以及附加的例如用户输入到对话框小部件之一的字符串。
我不想在一个地方编写一半的代码,直到我需要对话框的结果,而将其余代码写在另一地方,即对话框的 onClickListener 。
在某些情况下,第一对话框可能会调用第二对话框,例如指定不在第一对话框的ListView 列表中的颜色。
您的代码将分散在各处(在每个对话框的按钮 onClickListener 中),并且难以阅读或维护。
现在,在编写了一些像这样不清楚的代码之后,我想出了以下解决方案,该解决方案当然尊重 Android 设计指南。
我没有直接显示对话框,而是创建了一个处理消息的 Handler 派生类。
我向它发送第一条消息,该消息创建并显示一个对话框。它还将处理程序转发到对话框,并且其 onStop 方法中的 diaolg 向处理程序发送另一条消息,指示对话框的结束。您可以在其中检查对话框属性、编辑字段的内容或是否通过“确定”或“取消”停止。
现在,在消息处理程序中,任务的所有逻辑都位于消息 arg1 值的不同情况下。
某些情况可能会被跳过(例如,用户选择了标准颜色并且不需要特殊的颜色对话框)。
这些对话框独立于调用它们的场景,并且在其代码中仅反映其简单任务(从列表、某些复选框等中进行选择)。它们可以在其他场景中重用。
遵循一种模板如何使用这种方法:
}
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:
}