2016-06-12 41 views
0

我想在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

我想你忘了.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(); 
} 
+0

非常感謝! – purplewind