How to prevent dialog to cancel automatically
I have created a dialog. Now what I want is that onClick
of the ok button of my dialog I don't want it to close. But here it is automatically closed. So how can I prevent the dialog to close automatically? The code I have written is:
AlertDialog.Builder startDialog = new AlertDialog.Builder(this);
TextView txtStrtDialogTitle = new TextView(this);
txtStrtDialogTitle.setText(res.getString(R.string.strt_dialog_title));
txtStrtDialogTitle.setBackgroundResource(R.drawable.strt_dlg_tlt_bckgrnd);
txtStrtDialogTitle.setGravity(Gravity.CENTER);
txtStrtDialogTitle.setTextColor(Color.WHITE);
txtStrtDialogTitle.setTextSize(20);
TextView txtStrtDialogDesc = new TextView(this);
txtStrtDialogDesc开发者_开发知识库.setText(Html.fromHtml(res.getString(R.string.strt_dialog_desc)));
txtStrtDialogDesc.setPadding(10, 10, 10,10);
txtStrtDialogDesc.setBackgroundResource(R.drawable.strt_dlg_desc_bckgrnd);
txtStrtDialogDesc.setGravity(Gravity.CENTER);
txtStrtDialogDesc.setTextColor(Color.WHITE);
txtStrtDialogDesc.setTextSize(15);
startDialog.setCustomTitle(txtStrtDialogTitle);
startDialog.setView(txtStrtDialogDesc);
startDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id){
txtStrtDialogTitle.setText("SET PASSWORD"); }
});
AlertDialog alert = startDialog.create();
alert.show();
Dialogs are meant to close at onClick events. Either you reopen it (simple but strange for users, even frustrating I think) or you build your "dialog" inside an activity with dialog theme and have full control over click and close behaviors.
Regards, Stéphane
精彩评论