MVVM中的AlertDialog

发布于 2025-02-12 17:45:40 字数 1859 浏览 1 评论 0原文

我正在以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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走就滚别墨迹 2025-02-19 17:45:40
  1. 这是在片段本身中使用对话框代码的好习惯吗?
    是的,片段是一个UI层。在那里使用AlertDialog是正确的。

  2. 无论如何是否有清晰的
    这完全取决于您的实现,但是当前实现没有错,因为它是可读& 可以理解的

  3. 我有什么办法使对话框代码短?
    所以我不必一遍又一遍地编写相同的代码? (提醒:我正在使用3种不同样式的对话)

    是的,如果您使用相同的策略来创建对话框,那么只有某些样式是不同的,则可以创建具有相关参数的简单方法:

fun showDialogWith(style: String) {
    val styleTypeView = when(style) {
        "normal" -> R.layot.normal_dialog
        "info" -> R.layot.info_dialog
        "error" -> R.layot.error_dialog
    }

    layoutInflater.inflate(styleTypeView, null);
    
    /* Handle other theming / operations depending on the view type */
}


普通对话框 - showdialogwith(“ normal”)
信息。对话框 - showdialogwith(“ info”)
错误对话框 - showdialogwith(“错误”)

  1. 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.

  2. 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.

  3. 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:

fun showDialogWith(style: String) {
    val styleTypeView = when(style) {
        "normal" -> R.layot.normal_dialog
        "info" -> R.layot.info_dialog
        "error" -> R.layot.error_dialog
    }

    layoutInflater.inflate(styleTypeView, null);
    
    /* Handle other theming / operations depending on the view type */
}

And call it like:
Normal Dialog - showDialogWith("normal")
Info. Dialog - showDialogWith("info")
Error Dialog - showDialogWith("error")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文