AlertDialog按钮需要双击

时间:2012-11-09 05:40:44

标签: android 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;
                }
            });

1 个答案:

答案 0 :(得分:1)

问题是,覆盖onKey()会为您的Activity注册两个事件,KEY_DOWNKEY_UP以获取给定密钥。因此,在这两个事件中,您都会触发AlertDialog两次。我建议您覆盖onKeyDown()方法并将代码移到那里。希望这会有所帮助。