如何在这里实现Progressdialog?

时间:2011-11-14 09:19:34

标签: android

我正在从绘图画布中保存imae。 保存到SD卡需要几秒钟的时间。 因此,在保存过程中,我想显示图像保存的对话框...

那么如何实现进度对话框?

图像保存的代码如下所示:

 case R.id.saveBtn:
            Toast.makeText(getApplicationContext(), "Save", Toast.LENGTH_SHORT).show();
            final Activity currentActivity  = this;
            Handler saveHandler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    final AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
                    alertDialog.setTitle("Drawing App");
                    alertDialog.setMessage("Your drawing had been saved :)");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }
            } ;

           new ExportBitmapToFile(this,saveHandler, drawingSurface.getBitmap()).execute();

        break;

3 个答案:

答案 0 :(得分:2)

由于您使用的是AsyncTaskExportBitmapToFile),因此您可以在ProgressDialog中致电preExecute(),并在postExecute方法中将其解除。执行此操作不需要Handler

修改

class ExportBitmapToFile extends AsyncTask<...> {
    private ProgressDialog  m_progressDialog = null;
    .... 

    @Override
    protected void onPreExecute(){
        m_progressDialog = new ProgressDialog(m_context);
        m_progressDialog.setMessage("please wait...");
        m_progressDialog.setCancelable(false);
        m_progressDialog.show();
    }

    @Override
    protected void onPostExecute(HashMap<String, String> result){
        m_progressDialog.dismiss();
        //your alert dialog with message to user
        AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity);
            builder.setTitle("Drawing App");
            builder.setMessage("Your drawing had been saved :)");
            builder.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    return;
                }
            });
        alertDialog = builder.create()
        alertDialog.show();
    } 
}

答案 1 :(得分:1)

看起来ExportBitmapToFile是一个AsyncTask,在这种情况下在saveHandler.sendEmptyMessage(0);中调用onPreExecute()

您还可以在savehandler.sendEmptyMessage(1)中调用onPostExecute()并使alertDialog成为您活动的成员变量(而不是本地实例),并查看msg.what函数中的handleMessage(Message msg); - 0表示显示对话框,1表示对话框应隐藏。此外,您可能希望清除活动onStop

中的对话框

您还需要在调用create()之前在AlertDialog.Builder上设置所有属性:

private AlertDialog alertDialog = null; 
    Handler saveHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 0 && alertDialog != null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity);
                builder.setTitle("Drawing App");
                builder.setMessage("Your drawing had been saved :)");
                builder.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        return;
                    }
                });
                alertDialog = builder.create()
                alertDialog.show();
            }
            else if(alertDialog != null){
                alertDialog.dismiss();
                alertDialog = null;
            }
        }
    } ;

注意 - 这不会显示进度指示器 - 您应该显示ProgressDialog onPreExecute,将其关闭,然后在PostExecute上显示上述AlertDialog(使用相同的技术)。

答案 2 :(得分:1)

您应该在ExportBitmapToFile AsyncTask类中使用onPreExecute和onPostExecute,因此您应该向该类添加类似的内容。

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();

        }
        super.onPostExecute(result);
    }



    @Override
    protected void onPreExecute() {

        progressDialog = ProgressDialog.show(context, "Message",
                "Loading");

        super.onPreExecute();

    }