0

我有一个按钮,在女巫我做一些计算时显示一个 AlertDialog,这需要一些时间,我想在计算时显示一个进度条。这是我到目前为止所拥有的,但它并没有像我想要的那样工作。对话框显示,但在 AlertDialog 完成计算之前感到绝望

这是代码

public void OnClick(View v) {

    try {

        new DownloadingProgressTask().execute();

    } catch (Exception e) {

        Log.v("Exception", e.toString());
    }

}
    private class DownloadingProgressTask extends
        AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(Calculations.this);

    /** progress dialog to show user that the backup is processing. */

    /** application context. */

    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();

    }

    @SuppressWarnings("deprecation")
    protected Boolean doInBackground(final String... args) {
        try {

            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflatedview = layoutInflater.inflate(R.layout.calculationhelp, null);

            final TextView result = (TextView) inflatedview.findViewById(R.id.textViewresult);
            titleAlerDialog = (TextView) inflatedview.findViewById(R.id.textViewTitle);
            messageAlerDialog = (TextView) inflatedview.findViewById(R.id.textViewMsg);

            separated = help.toString().split("#");

            Resources r = getResources();
            Bundle extras = getIntent().getExtras();
            if (extras != null) {

                        tableResults = r.getStringArray(R.array.Calculo1Results1);
                        result.setText(r.getString(R.string.calculo1results1));
            }

            titleAlerDialog.setText(separated[0]);

            messageAlerDialog.setText(separated[1].toString());


            /* Find Tablelayout defined in main.xml */
            TableLayout tl = (TableLayout) inflatedview.findViewById(R.id.myTableLayout);
            tl.setColumnStretchable(0, true);
            tl.setColumnStretchable(1, true);
            tl.setColumnStretchable(2, true);

            for (int i = 0; i < tableResults.length; i++) {
                /* Create a new row to be added. */
                TableRow tr = new TableRow(Calculations.this);
                tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                /* Create a cell1 to be the row-content. */
                TextView cell1 = new TextView(Calculations.this);
                TextView cell2 = new TextView(Calculations.this);
                TextView cell3 = new TextView(Calculations.this);

                String[] cells = tableResults[i].toString().split("@");

                cell1.setWidth(130);
                cell1.setGravity(Gravity.CENTER_VERTICAL
                        | Gravity.CENTER_HORIZONTAL);
                cell1.setText(cells[0].toString());
                cell1.setBackgroundResource(R.drawable.celda_cuerpo);
                cell1.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                cell2.setWidth(130);
                cell2.setGravity(Gravity.CENTER_VERTICAL
                        | Gravity.CENTER_HORIZONTAL);
                cell2.setText(cells[1].toString());
                cell2.setBackgroundResource(R.drawable.celda_cuerpo);
                cell2.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                cell3.setWidth(130);
                cell3.setGravity(Gravity.CENTER_VERTICAL
                        | Gravity.CENTER_HORIZONTAL);
                cell3.setText(cells[2].toString());
                cell3.setBackgroundResource(R.drawable.celda_cuerpo);
                cell3.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                /* Add cell1 to row. */
                tr.addView(cell1);
                tr.addView(cell2);
                tr.addView(cell3);

                /* Add row to TableLayout. */
                tl.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

            }
            //dialog.dismiss();
            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

@Override
protected void onPostExecute(final Boolean success) {

if (dialog.isShowing()) {
    dialog.dismiss();
}

if (success) {
    AlertDialog.Builder dialog1 = new AlertDialog.Builder(Calculations.this);
    dialog1.setNegativeButton("Close",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {

                }
            });

    dialog1.setView(inflatedview);
    final AlertDialog alert = dialog1.create();
    alert.requestWindowFeature(Window.FEATURE_NO_TITLE);

    alert.show();



    ((Button) alert.findViewById(android.R.id.button2))
            .setBackgroundResource(R.drawable.custom_button_red);
    ((Button) alert.findViewById(android.R.id.button2))
            .setTextColor(Color.WHITE);


} else {
    Toast.makeText(Calculations.this, "Error", Toast.LENGTH_LONG)
            .show();
}
}

}

任何帮助将不胜感激

4

1 回答 1

0

尝试使用以下内容。我已经向你的 AsyncTask 添加了一个构造函数,它接受一个上下文,并在整个过程中使用它来创建你的视图等。我还将第二个警报对话框的创建移到 doInBackground() 中,只留下了显示它的代码onPostExecute()。它应该加快速度。

public void OnClick(View v) {

    try {

        new DownloadingProgressTask(getBaseContext()).execute();

    } catch (Exception e) {

        Log.v("Exception", e.toString());
    }

}

private class DownloadingProgressTask extends AsyncTask<String, Void, Boolean> {

    Context mContext;

    public DownloadingProgressTask(Context c) {
        mContext = c;
    }

    private ProgressDialog dialog = new ProgressDialog(mContext);

    final AlertDialog alert;
    /** progress dialog to show user that the backup is processing. */

    /** application context. */

    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();

    }

    @SuppressWarnings("deprecation")
    protected Boolean doInBackground(final String... args) {
        try {

            LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            inflatedview = layoutInflater.inflate(R.layout.calculationhelp, null);

            final TextView result = (TextView) inflatedview.findViewById(R.id.textViewresult);
            titleAlerDialog = (TextView) inflatedview.findViewById(R.id.textViewTitle);
            messageAlerDialog = (TextView) inflatedview.findViewById(R.id.textViewMsg);

            separated = help.toString().split("#");

            Resources r = getResources();
            Bundle extras = getIntent().getExtras();
            if (extras != null) {

                tableResults = r.getStringArray(R.array.Calculo1Results1);
                result.setText(r.getString(R.string.calculo1results1));
            }

            titleAlerDialog.setText(separated[0]);

            messageAlerDialog.setText(separated[1].toString());

            /* Find Tablelayout defined in main.xml */
            TableLayout tl = (TableLayout) inflatedview.findViewById(R.id.myTableLayout);
            tl.setColumnStretchable(0, true);
            tl.setColumnStretchable(1, true);
            tl.setColumnStretchable(2, true);

            for (int i = 0; i < tableResults.length; i++) {
                /* Create a new row to be added. */
                TableRow tr = new TableRow(mContext);
                tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                /* Create a cell1 to be the row-content. */
                TextView cell1 = new TextView(mContext);
                TextView cell2 = new TextView(mContext);
                TextView cell3 = new TextView(mContext);

                String[] cells = tableResults[i].toString().split("@");

                cell1.setWidth(130);
                cell1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                cell1.setText(cells[0].toString());
                cell1.setBackgroundResource(R.drawable.celda_cuerpo);
                cell1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                cell2.setWidth(130);
                cell2.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                cell2.setText(cells[1].toString());
                cell2.setBackgroundResource(R.drawable.celda_cuerpo);
                cell2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                cell3.setWidth(130);
                cell3.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                cell3.setText(cells[2].toString());
                cell3.setBackgroundResource(R.drawable.celda_cuerpo);
                cell3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                /* Add cell1 to row. */
                tr.addView(cell1);
                tr.addView(cell2);
                tr.addView(cell3);

                /* Add row to TableLayout. */
                tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

                AlertDialog.Builder dialog1 = new AlertDialog.Builder(mContext);
                dialog1.setNegativeButton("Close", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                });

                dialog1.setView(inflatedview);
                alert = dialog1.create();
                alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
            }
            // dialog.dismiss();
            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (success) {
            alert.show();

            ((Button) alert.findViewById(android.R.id.button2)).setBackgroundResource(R.drawable.custom_button_red);
            ((Button) alert.findViewById(android.R.id.button2)).setTextColor(Color.WHITE);

        } else {
            Toast.makeText(mContext, "Error", Toast.LENGTH_LONG).show();
        }

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

    }
}
于 2012-09-21T17:58:22.020 回答