Android:进度条对话框的代码

时间:2014-12-05 16:02:33

标签: android progressdialog

我正在编写一个android程序来测试android中不同类型的对话框。 除了包含进度条的对话框外,所有其他工作都完美无缺。代码工作,但进度条拒绝增加。我有下面的应用程序的源代码,也许有人可以帮我找出我做错了什么:

package com.example.progressdial;

//import com.example.alertdialogtest.R;

import android.support.v7.app.ActionBarActivity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        }


        //DIALOG NO 4
        //This method starts a progressBar Dialog
        public void startProgressBarDialog(View v){
            //create a new instance of the progressDialog class..
            final ProgressDialog progressBarDialog= new ProgressDialog(this);

            //set the icon, title and progress style..
            progressBarDialog.setIcon(R.drawable.ic_launcher);
            progressBarDialog.setTitle("Downloading files...");
            progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

            //set the OK and the CANCEL Buttons..

                //setting the OK Button
                progressBarDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,
                            int whichButton){
                        Toast.makeText(getBaseContext(),
                                "OK clicked!", Toast.LENGTH_SHORT).show();
                    }
                });

                //set the Cancel button
                progressBarDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog, int whichButton){
                        Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
                    }
                });

            //initialise the progressBar
            progressBarDialog.setProgress(0);
            new Thread(new Runnable(){
                public void run(){
                    for (int i=0; i<=15; i++){
                        try{
                            Thread.sleep(1000);
                            progressBarDialog.incrementProgressBy((int)(100/15));
                        }
                        catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                    //dismiss the dialog
                    progressBarDialog.dismiss();
                 }
               });
            //show the dialog
            progressBarDialog.show();
        }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

你忘了开始你的线程。尝试添加以下开头:

new Thread(new Runnable(){
            public void run(){
                for (int i=0; i<=15; i++){
                    try{
                        Thread.sleep(1000);
                        progressBarDialog.incrementProgressBy((int)(100/15));
                    }
                    catch(InterruptedException e){
                        e.printStackTrace();
                    }
                }
                //dismiss the dialog
                progressBarDialog.dismiss();
            }
        }).start(); <------------------------

看看它是否有效

相关问题