如何在活动中显示自定义对话框?

时间:2016-06-03 10:45:46

标签: android dialog android-alertdialog customdialog

如上所述。

我有自定义Dialog(我想应该通过不同的类扩展...)

public class ResetPasswordDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_lost_password, null, false);
        ButterKnife.bind(this, v);

        final AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.change_password)
                .setCancelable(false)
                .setView(v)
                .setPositiveButton(R.string.change_password, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //
                    }
                })
                .create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface arg0) {

                Button okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                okButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO: check if password is correct
                    }
                });
            }
        });
        return dialog;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以在OneActivityClass上创建方法。像

public static void showDialog()
{
// YOUR CUSTOM DIALOG CODE HERE

}

然后你可以打电话到你想要的地方。像

 OneActivityClass.showDialog();

假设MyActivity有,

public void showDialog(String title,String message)
    {
        Builder builder = new Builder(global_context);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.show();
    }
 //this is not custom dialog // intead of this method you can put your custom Dialog code

然后打电话给你MainActivity,

MyActivity.showDialog("YOUR TITLE","YOUR MESSAGE");

这可能对你有所帮助。

答案 1 :(得分:0)

我猜dialog.show();遗失了。

请参阅以下内容:http://android-developers.blogspot.com/2012/05/using-dialogfragments.html