方法调用后不会出现对话框

时间:2016-06-12 16:42:05

标签: android dialog

我正在尝试在android中构建一个对话框,但在调用该方法后它不会出现。

这就是我声明方法的方法:

public Dialog onCreateDialog() {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("testing")
            .setPositiveButton("COPY TO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do something
                }
            })
            .setNegativeButton("MOVE TO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //do something
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
}

这就是我称之为方法的方式:

for (int i = 0; i < lvMain.getChildCount(); i++) {
            LinearLayout itemLayout = (LinearLayout) lvMain.getChildAt(i);
            final CheckBox cb = (CheckBox) itemLayout.findViewById(R.id.cbBox);
            cb.setVisibility(View.VISIBLE);
            cb.setChecked(true);
            onCreateDialog();
        }

知道为什么它不起作用?

1 个答案:

答案 0 :(得分:1)

我想你忘了 .show();

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("testing")
            .setPositiveButton("COPY TO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // do something
                }
            })
            .setNegativeButton("MOVE TO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //do something
                }
            }).show();
}