我遇到了無法隱藏的警報對話框的問題。如何在AlertDialog後顯示ProgressDialog
當用戶按下一個按鈕,我證明與此代碼創建一個對話框:
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」的方法進行了一些沉重的操作,和裏面我使用的是AysncTask那顯示一個ProgressDialog。
我遇到的問題是,對話框提示輸入密碼永遠不會出現在屏幕上(我試過用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()?當然,你必須調用它來顯示一個對話框。如果是,您是否可以修改代碼以獲得更多清晰度。 – VendettaDroid
你可以展示你的asynctask嗎? – wesdfgfgd
是的,我創建它後立即調用dialog.show()。你想讓我澄清哪一部分?我想我的setPositiveButton onClickListener部分有問題。 – richardtz