MVVM中的AlertDialog
我正在以MVVM模式构建一个应用程序,并且正在使用3种不同样式的对话框(其中一些具有2个按钮,其中一些按钮3)。 由于AlertDialog是指该视图,因此我在片段中编写了代码。 我在同一片段中有3个对话框,看起来有点混乱。
我的问题:
- 这是在片段本身中使用对话框代码的好习惯,就像我首先一样?
- 无论如何,是否有更清楚的?
- 有什么办法使对话框代码缩短?所以我不必一遍又一遍地编写相同的代码? (提醒:我使用的是3种不同样式的对话框)
这是对话框之一的代码:
AlertDialog optionsDialog = new AlertDialog.Builder(requireContext()).create();
View dialogView = getLayoutInflater().inflate(R.layout.tasks_long_click_options_design, null);
optionsDialog.setView(dialogView);
optionsDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // Transparent Corners
Button editButton = dialogView.findViewById(R.id.editTaskButton);
Button deleteButton = dialogView.findViewById(R.id.deleteTaskButton);
Button cancelButton = dialogView.findViewById(R.id.cancelTaskButton);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent editTask = new Intent(requireActivity(), AddTaskActivity.class);
editTask.putExtra("taskName", task.getTaskName());
editTask.putExtra("taskPriority", task.getTaskPriority());
editTask.putExtra("taskTag", task.getTaskTag());
editTask.putExtra("taskDate", task.getTaskDate());
editTask.putExtra("taskTime", task.getTaskTime());
editTask.putExtra("taskID", task.getTaskID());
Log.e("DailyFragment", "Send Task By Intent: Name- " + task.getTaskName() + " Color-" + task.getTaskPriority() + " Tag- " + task.getTaskTag() + " Date- "
+ task.getTaskDate() + " Time- " + task.getTaskTime());
optionsDialog.dismiss();
第二个对话框是2个按钮,第三个对话框与4个。
谢谢!
I am building an app in MVVM pattern and I am using 3 different styles of dialogs(some of them have 2 buttons,some of them 3).
Since the AlertDialog refers to the view, I wrote the code in the fragment.
I have 3 dialogs in the same fragment and its look kind of messy.
My questions:
- Is this a good practice to use the dialogs code in the fragment itself, like I did in the first place?
- Is there anyway to make it clearer ?
- Is there any way I make the dialog code shorter? So I don't have to write the same code over and over? (Reminder: I am using 3 different styles of dialogs)
This is the code for one of the dialogs:
AlertDialog optionsDialog = new AlertDialog.Builder(requireContext()).create();
View dialogView = getLayoutInflater().inflate(R.layout.tasks_long_click_options_design, null);
optionsDialog.setView(dialogView);
optionsDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // Transparent Corners
Button editButton = dialogView.findViewById(R.id.editTaskButton);
Button deleteButton = dialogView.findViewById(R.id.deleteTaskButton);
Button cancelButton = dialogView.findViewById(R.id.cancelTaskButton);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent editTask = new Intent(requireActivity(), AddTaskActivity.class);
editTask.putExtra("taskName", task.getTaskName());
editTask.putExtra("taskPriority", task.getTaskPriority());
editTask.putExtra("taskTag", task.getTaskTag());
editTask.putExtra("taskDate", task.getTaskDate());
editTask.putExtra("taskTime", task.getTaskTime());
editTask.putExtra("taskID", task.getTaskID());
Log.e("DailyFragment", "Send Task By Intent: Name- " + task.getTaskName() + " Color-" + task.getTaskPriority() + " Tag- " + task.getTaskTag() + " Date- "
+ task.getTaskDate() + " Time- " + task.getTaskTime());
optionsDialog.dismiss();
The second dialog is with 2 buttons,and the third dialog is with 4.
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是在片段本身中使用对话框代码的好习惯吗?
是的,片段是一个UI层。在那里使用AlertDialog是正确的。
无论如何是否有清晰的?
这完全取决于您的实现,但是当前实现没有错,因为它是可读& 可以理解的。
我有什么办法使对话框代码短?
所以我不必一遍又一遍地编写相同的代码? (提醒:我正在使用3种不同样式的对话)
是的,如果您使用相同的策略来创建对话框,那么只有某些样式是不同的,则可以创建具有相关参数的简单方法:
:
普通对话框 -
showdialogwith(“ normal”)
信息。对话框 -
showdialogwith(“ info”)
错误对话框 -
showdialogwith(“错误”)
Is this a good practice to use the dialogs code in the fragment itself?
Yes, the Fragment is a UI layer. It is correct to use an AlertDialog there.
Is there anyway to make it clearer?
That completely depends on your implementation, however there is nothing wrong with the current implementation as it is readable & quite understandable.
Is there any way I make the dialog code shorter?
So I don't have to write the same code over and over? (Reminder: I am using 3 different styles of dialogs)
Yes, if you are using same strategy for dialog creation where only certain styles are different then you can create simple method with relevant parameters like:
And call it like:
Normal Dialog -
showDialogWith("normal")
Info. Dialog -
showDialogWith("info")
Error Dialog -
showDialogWith("error")