当一个活动加载其他活动时添加进度条

时间:2012-04-04 03:48:32

标签: android android-activity progress-bar

这是我的活动oncreate()方法。我设置了一个带有对话窗口的正按钮ok。 当我点击它时,它会导航UI活动staticDisplay.class。我想在对话窗口中单击ok后设置进度条,直到它加载下一个活动。

public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "ACTIVITY ONCREATE");
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.dialog);
    setContentView(R.layout.main);

    if (Registration.isRunning == false) {

        TextView title = new TextView(this);
        title.setText("DM2");
        title.setBackgroundColor(Color.DKGRAY);
        title.setPadding(10, 10, 10, 10);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setTextSize(20);
        /* alert message */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCustomTitle(title);

        builder.setMessage(R.string.app_description).setPositiveButton(
                "Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (Registration.isRunning == false) {

                            startService(new Intent(
                                    getApplicationContext(),
                                    Registration.class));
                        }
                        staticInfo();
                        if (Registration.ruid == null)
                            Registration.ruid = uId;

                        startActivity(new Intent(getApplicationContext(),
                                StatisticDisplay.class));
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    } else {
        startActivity(new Intent(getApplicationContext(),
                StatisticDisplay.class));
    }
}

创建方法

上的StaticDisplay.class
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statdisplay);


    usage_btn = (Button) findViewById(R.id.appstat);
    usage_btn.setOnClickListener(this);
    setting_btn = (Button) findViewById(R.id.setting);
    setting_btn.setOnClickListener(this);
}

3 个答案:

答案 0 :(得分:2)

您可以使用AsyncTask进行后台处理并显示progressdialog,如下所示,

打电话给班级

        new Task().execute(CurrentActivity.this);  

在任务类中:

        private class Task extends AsyncTask<Void, Void, Void> 
             { 
                 ProgressDialog dialog;
                 Context context;
                public Task(Context context)
                { 
                  this.context=context;
                   //constructor for this class
                }
               protected void onPreExecute() {            
                 //create the progress dialog as 
                   dialog=new ProgressDialog(context);
              } 
             protected Void doInBackground(Void... JSONArray) { 

           //Place your background process code     

            }

            protected void onPostExecute(Void unused) {
                 //dismiss the progressdialog 
                dialog.dismiss();
           } 
       }

答案 1 :(得分:2)

本准则,为我工作

private ProgressDialog progress;
Button CallButton;

@Override
    protected void onCreate(Bundle savedInstanceState) 
    {
     progress = new ProgressDialog(Page.this);
     CallButton=(Button)findViewById(R.id.button1);

      CallButton.setOnClickListener(this);

   }

@Override
    public void onClick(View v)
    {
        switch (v.getId()) {
        case R.id.button1:
             progress.setMessage("Please Wait Loading...");
       progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
       progress.setIndeterminate(true);
    progress.show();
     new Thread() 
     {

         public void run() 
         {

             Intent i = new Intent(Page.this,Update.class);
             startActivity(i);
             progress.dismiss();
         }

     }.start();


            break;
}
}

答案 2 :(得分:0)

请查看以下代码段

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your loading code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {

         progDailog.dismiss();
         }
 }

在run()方法中,您可以输入加载代码。