如何防止对话框自动取消
我创建了一个对话框。现在我想要的是对话框的确定按钮的 onClick
我不希望它关闭。但这里自动关闭了。那么如何防止对话框自动关闭呢?我写的代码是:
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对话框应在 onClick 事件时关闭。要么重新打开它(简单但对用户来说很奇怪,我认为甚至令人沮丧),要么在具有对话框主题的活动中构建“对话框”,并完全控制单击和关闭行为。
问候,
史蒂芬
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