我可以从Activity获得对话框吗?

时间:2015-07-28 04:53:44

标签: android android-activity android-alertdialog

我有一个函数活动作为参数。我尝试返回此活动上显示的对话框,我可以这样做吗?

private Dialog getDialogFromActivity(Activity activity){
     Dialog dialog = null;
     //code get dialog from activity here
     return dialog;
}

4 个答案:

答案 0 :(得分:0)

或者你可以创建一个对话框,你可以从任何地方调用它:

public class AlertDialogManager {
    /**
     * Function to display simple Alert Dialog
     * @param context - application context
     * @param title - alert dialog title
     * @param message - alert message
     * @param status - success/failure/info (used to set icon)
     *               - pass null if you don't want icon
     * */
    @SuppressWarnings("deprecation")
    public void showAlertDialog(Context context, String title, String message,
            String icon) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        if(icon == "fail"){
            alertDialog.setIcon(R.drawable.fail);
        }else if(icon == "true"){
            alertDialog.setIcon(R.drawable.success);
        }else if(icon == "info"){
            alertDialog.setIcon(R.drawable.info);
        }

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

答案 1 :(得分:0)

如果您的对话框位于DialogFragment中,则可以这样:

@Nullable
public static DialogFragment getDialogFragment(@NonNull FragmentManager 
fragmentManager) {
    Fragment fragment = fragmentManager.getPrimaryNavigationFragment();
    if (fragment instanceof DialogFragment) {
        DialogFragment dialogFragment = (DialogFragment) fragment;
        if (dialogFragment.getShowsDialog()) {
            return dialogFragment;
        }
    }

    if (fragment != null) {
        return getDialogFragment(fragment.getChildFragmentManager());
    }

    List<Fragment> fragments = fragmentManager.getFragments();
    int count = fragments.size();
    if (count > 0) {
        fragment = fragments.get(count -1);

        if (fragment instanceof DialogFragment) {
            DialogFragment dialogFragment = (DialogFragment) fragment;
            if (dialogFragment.getShowsDialog()) {
                return dialogFragment;
            }
        }

        if (fragment != null) {
            return getDialogFragment(fragment.getChildFragmentManager());
        }
    }

    return null;
}

然后

public Dialog getDialogFromActivity(Activity activity) {
    DialogFragment dialogFragment = getDialogFragment(activity.getSupportFragmentManager());
    if (dialogFragment != null) {
        return dialogFragment.getDialog;
    }
    return null;
}

答案 2 :(得分:0)

如果有人需要更简单实用的解决方案:

public void onStart() {//or inside other Lifecycle method of DialogFragment
        super.onStart();

        Context ctxt = getContext();

        final AlertDialog dialog = (AlertDialog) getDialog();

        if(ctxt instanceof YourActivity) {
            ((YourActivity) ctxt).saveDialogs = dialog;
        }
    }

YourActivity内创建变量public saveDialogs Dialog;,然后在其上调用Dialog方法,例如if(saveDialogs != null) {saveDialogs.dismiss();}

答案 3 :(得分:-1)

如果您想在活动中创建一个对话框,则可以投射您的活动。

private Dialog getDialogFromActivity(Activity activity){
     Dialog dialog = null;
     NameOfYourClassActivity act = (NameOfYourClassActivity)activity;
     dialog = act.getNameOfyourFieldDialog();
     return dialog;
}

这是你想要的吗?