添加 aLinearLayout
应该可以正常工作:
在#onCreate(Bundle)
:
...
...
/*LinearLayout*/ mDlgLayout = new LinearLayout(this);
mDlgLayout.setOrientation(LinearLayout.VERTICAL);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Some title");
alert.setView(mDlgLayout);
alert.setNeutralButton("Regenerate", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int which) {
// onClick will dismiss the dialog, just posting delayed
// to pop up the dialog again & change the layout.
mDlgLayout.postDelayed(new Runnable() {
public void run() {
alterDlgLayout();
}
}, 200L);
}
});
/*AlertDialog*/ mDlg = alert.create();
...
...
在#alterDlgLayout()
void alterDlgLayout() {
mDlgLayout.removeAllViews();
Random rnd = new Random(System.currentTimeMillis());
int n = rnd.nextInt(3) + 1;
for (int i = 0; i < n; i++) {
EditText txt = new EditText(this);
txt.setHint("Some hint" + rnd.nextInt(100));
mDlgLayout.addView(txt);
}
mDlgLayout.invalidate();
mDlg.show();
}
在#onResume()
alterDlgLayout();
在#onPause()
mDlg.dismiss();