简体   繁体   English

Android 对话框显示取消的进度阶段

[英]Android dialog showing stages of progress with a cancel

Is it possible to create a custom dialog on Android depicting stages, like three images, as each stage is achieved, the image changes color, until all images change color and the dialog closes.是否可以在 Android 上创建一个自定义对话框来描绘阶段,例如三个图像,随着每个阶段的实现,图像会改变颜色,直到所有图像都改变颜色并且对话框关闭。 I also want an opportunity for a user to cancel via a cancel button.我还希望用户有机会通过取消按钮取消。 To accomplish this I need to have the app communicate to the dialog box, and for the dialog box to be able to communicate with the calling fragment.为此,我需要让应用程序与对话框进行通信,并使对话框能够与调用片段进行通信。 I know the latter is true, but can the fragment communicate with the dialog beyond opening it?我知道后者是正确的,但是片段可以在打开对话框之外与对话框进行通信吗? Is there a good example of this?有这方面的好例子吗?

You can create your own dialog with UI that you expect and there are many ways to achieve that.您可以使用您期望的 UI 创建自己的对话框,并且有很多方法可以实现这一点。 I prefer following way with DialogFragment, an convenience dialog class from Android framework:我更喜欢使用 DialogFragment 的以下方式,DialogFragment 是 Android 框架中的一个便捷对话框类:

public class SampleDialog extends DialogFragment {
    public SampleDialog(){
        super();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
         View view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_your_dialog_layout, null);//your custom layout
         Button btnYes = view.findViewById(R.id.btnYes); //assume the customized layout have two buttons (yes and no)
         Button btnNo = view.findViewById(R.id.btnNo);
         btnNo.setOnClickListener(view->{
             //your business code
             dismiss();
         })
         AlertDialog alertD = builder.create();
         alertD.setView(view);
         return alertD;
    }
}

Then, call dialog from activity:然后,从活动调用对话框:

SampleDialog dialog = new SampleDialog();
dialog.show(getSupportFragmentManager(),"tag");

If you want to call dialog from fragment:如果要从片段调用对话框:

SampleDialog dialog = new SampleDialog();
dialog.show(getActivity().getSupportFragmentManager(),"tag"); 

Refer to this link to see more DialogFragment methods: https://developer.android.com/reference/android/app/DialogFragment请参阅此链接以查看更多 DialogFragment 方法: https : //developer.android.com/reference/android/app/DialogFragment

UPDATE:更新:

To pass data from dialog to activity, simply use interface, I found a good guide: https://github.com/codepath/android_guides/wiki/Using-DialogFragment#passing-data-to-activity要将数据从对话框传递到活动,只需使用界面,我找到了一个很好的指南: https : //github.com/codepath/android_guides/wiki/Using-DialogFragment#passing-data-to-activity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM