【发布时间】:2012-09-18 19:42:14
【问题描述】:
我无法隐藏一个警告对话框。
当用户按下按钮时,我会显示一个使用此代码创建的对话框:
new AlertDialog.Builder(this)
.setTitle(R.string.enterPassword)
.setView(textEntryView)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String password = pwdText.getText().toString();
dialog.dismiss();
processUserAction(password,targetUri);
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.
create();
在“processUserAction”方法中执行了一些繁重的操作,在其中我使用了一个显示 ProgressDialog 的 AysncTask。
我遇到的问题是提示输入密码的对话框永远不会出现在屏幕上(我尝试过用dismiss()、cancel())。 我猜它会一直留在那里直到 onClick 方法完成。
那么,我的问题是如何关闭该 AlertDialog,以便显示 ProgressDialog?
我一直在尝试的另一种方法是在 AlertDialog 中设置 DismissListener 并从那里调用繁重的操作,但我没有运气(它没有被调用)。
编辑:添加 AsyncTask 代码
public class BkgCryptOperations extends AsyncTask<File,Void,Integer>{
@Override
protected Integer doInBackground(File... files) {
if (files!=null && files.length > 0){
File source = files[0];
File target = files[1];
return cryptAction.process(source,password, target);
}
return Constants.RetCodeKO;
}
CryptAction cryptAction;
String password;
ProgressDialog progressDialog;
public BkgCryptOperations (CryptAction cryptAction,String password,ProgressDialog progressDialog){
this.cryptAction=cryptAction;
this.password=password;
this.progressDialog=progressDialog;
}
@Override
protected void onPreExecute() {
if (progressDialog!=null){
progressDialog.show();
}
}
protected void onPostExecute(Integer i) {
if (progressDialog!=null){
progressDialog.dismiss();
}
}
}
提前致谢
【问题讨论】:
-
你在调用 dialog.show() 吗?当然,你必须调用它来显示一个对话框。如果是,您能否修改代码以使其更清晰。
-
是的,我在创建 dialog.show() 后立即调用它。你要我澄清哪一部分?我认为我的 setPositiveButton onClickListener 部分有问题。
-
您是否尝试过在 Activity 中定义静态 AlertDialog,然后从 onClick() 事件中直接通过其名称将其关闭(而不是使用该 DialogInterface 实例)?
-
您的意思是在 Activity 中引用 AlertDialog 并在该引用上执行关闭,对吗?我会检查并更新。感谢您的建议。
标签: java android android-asynctask android-alertdialog progressdialog