水平ProgressDialog未在android中显示

时间:2013-12-05 17:38:45

标签: android progressdialog

我有MainActivity:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bo = new Operation(getApplicationContext());
    }

    public void op_perform(View v) throws Exception { //call this when a button is pressed
        try {
            bo.demo();
        } catch (Exception e) {
            // TODO: handle exception
        }

Operation类包含以下代码行:

Context con;

Operation(Context ac) {
        con = ac;
        barProgressDialog = new ProgressDialog(con);
        updateBarHandler = new Handler();

    }


    public void demo() {
        barProgressDialog = new ProgressDialog(con);
        barProgressDialog.setTitle("Downloading Image ...");
        barProgressDialog.setMessage("Download in progress ...");
        barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);
        barProgressDialog.setProgress(0);
        barProgressDialog.setMax(20);
        barProgressDialog.show();
            ...

但是,我在屏幕上根本没有得到进度对话框。我希望进度对话框是一个确定的横向对话框。这只是一段代码,我需要把它们放在Asyn任务中。但是processdialog甚至没有显示出来。推荐解决方案我是android的初学者。

1 个答案:

答案 0 :(得分:0)

以下是您需要的完整示例:http://turboprogramacion.blogspot.cl

private ProgressDialog barraProgreso;
private Handler handler;

private void mostrarBarraProgreso(){
    barraProgreso = new ProgressDialog(MainActivity.this);
    barraProgreso.setTitle("Buscando...");
    barraProgreso.setMessage("Progreso...");
    barraProgreso.setProgressStyle(barraProgreso.STYLE_HORIZONTAL);
    barraProgreso.setProgress(0);
    barraProgreso.setMax(10);
    barraProgreso.show();

    handler = new Handler();

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (barraProgreso.getProgress() <= barraProgreso.getMax()) {
                    Thread.sleep(1000);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            barraProgreso.incrementProgressBy(1);
                        }
                    });

                    if(barraProgreso.getProgress() == barraProgreso.getMax()){
                        barraProgreso.dismiss();
                    }
                }
            }catch (InterruptedException er){
                er.printStackTrace();
            }
        }
    }).start();
}