1
我正在顯示用於在我的應用程序中下載文件的進度對話框,但如果用戶需要取消下載,那麼他將不得不按下後退按鈕,然後彈出警報有兩個按鈕的對話框。問題是我必須雙擊警報對話框的按鈕,然後只有警報對話框被取消。爲我提出任何解決方案。AlertDialog按鈕需要雙擊
這裏是供您參考代碼和平..
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
pDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK){
running = false;
/*Intent intent = new Intent(context, NewDialog.class);
startActivity(intent);*/
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setTitle("Ariisto");
alertDialog.setMessage("Do you Want to Cancel the Download ?");
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File externalFile = new File(Environment.getExternalStorageDirectory(),"downloadedfile.pdf");
externalFile.delete();
pDialog.dismiss();
running = false;
Log.d("External File", "DELETED");
pDialog.setProgress(0);
count = 2;
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
new DownloadFileFromURL().execute(file_url);
running = true;
count = 0;
}
});
AlertDialog alert = alertDialog.create();
alert.show();
}
return false;
}
});
嗨Egor ..謝謝你的建議,但是我不能重寫onKeyDown(),因爲它只顯示onKey()在setOnKeyListener中。你能告訴我片段代碼onKeyDown()上面的片段..否則重定向我任何好的教程或任何可能有用的鏈接。謝謝。 –
我也嘗試重寫onKeyDown()方法的活動,但仍然給它相同的結果。 –
@NitinBathija,是的,我的意思是活動的onKeyDown()方法。 – Egor