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

问题描述 投票:-1回答:1

是否有可能在Android上创建一个自定义对话框来描述各个阶段,例如三张图像,每个阶段实现时,图像都会更改颜色,直到所有图像更改颜色并且对话框关闭为止。我还希望用户有机会通过“取消”按钮进行取消。为此,我需要使该应用程序与对话框进行通信,并使该对话框能够与调用片段进行通信。我知道后者是正确的,但是片段可以在不打开对话框的情况下与对话框进行通信吗?有一个很好的例子吗?

android android-dialogfragment android-dialog
1个回答
0
投票

您可以使用期望的UI创建自己的对话框,并且有很多方法可以实现。我更喜欢使用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;
    }

    public void show(FragmentManager fragmentManager){
        show(fragmentManager,"dialog");
    }
    public void hide(){
        dismiss();
    }
}

然后,从活动中调用对话框:

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

如果要从片段调用对话框:

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

请参阅此链接以查看更多DialogFragment方法:https://developer.android.com/reference/android/app/DialogFragment

UPDATE:

要将数据从对话框传递到活动,只需使用界面,我找到了一个很好的指南:https://github.com/codepath/android_guides/wiki/Using-DialogFragment#passing-data-to-activity

© www.soinside.com 2019 - 2024. All rights reserved.