Android ProgressDialog停止旋转

时间:2012-03-09 04:08:31

标签: android progressdialog

我在Android应用程序中创建了一个ProgressDialog。但我遇到的问题是在实际工作的时候停止旋转车轮。这是我的代码。我怎样才能让它在我的其他工作进行的同时不断旋转?

button5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
    System.out.println("Button5");

    //Handler to make the please wait message
    final ProgressDialog myProgressDialog = ProgressDialog.show(
            FoodSubstitutesActivity.this, "Please wait...",
            "Getting most recent updates...", true);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            //DO STUFF - STOPS SPINNING WHEEL UNTIL THIS PART IS COMPLETE. 
            myProgressDialog.dismiss();
        }

    }, 500);
}
}); 

2 个答案:

答案 0 :(得分:1)

为什么不尝试这样做?

final ProgressDialog dialog = ProgressDialog.show(this, "Title", 
"Message", true);
final Handler handler = new Handler() {
   public void handleMessage(Message msg) {
      dialog.dismiss();
      }
   };
Thread checkUpdate = new Thread() {  
   public void run() {
//
// YOUR LONG CALCULATION (OR OTHER) GOES HERE
//
      handler.sendEmptyMessage(0);
      }
   };
checkUpdate.start();

取自:http://www.tutorials- android.com/learn/How_to_display_a_progress_dialog_while_computing.rhtml

答案 1 :(得分:0)

使用此代码可能对您有帮助,

                // TODO Auto-generated method stub
                myProgressDialog = ProgressDialog.show(MainActivity.this,
                        "", "Please wait....");
                myProgressDialog
                        .setProgressStyle(ProgressDialog.STYLE_SPINNER);

                new Thread() {
                    public void run() {
                        try {

                            Thread.sleep(1000);
                        } catch (Exception e) {
                        }

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                               // do your action...........
                                finish();

                            }
                        });
                        // Dismiss the Dialog
                        myProgressDialog.dismiss();
                    }
                }.start();

...谢谢