1

进度条对我不起作用。我试图展示一些正在进行的细节。但我不能,我做错了什么?请帮我。

活动文件名 - MainActivity.java

package com.example.sqliteexample;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;

public class MainActivity extends Activity {
private Handler handler = new Handler();
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //dialog = ProgressDialog.show(this,getString(R.string.loading) ,getString(R.string.alignMsg));
    dialog = ProgressDialog.show(this,"Loading", "Loading the Image of the Day");
    Thread th = new Thread()
    {
        public void run() {
            handler.post(
                    new Runnable (){
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            dialog.dismiss();
                        }});
        }};
        th.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

4

2 回答 2

0

尝试使用handler.postDelayed

dialog = ProgressDialog.show(this,
         "Loading", "Loading the Image of the Day");
handler.postDelayed(runnable,10000);
}
Runnable runnable = new Runnable() {
   @Override
   public void run() {
      /* dismiss  dialog here*/
      dialog.dismiss();

      handler.removeCallbacks(runnable);
   }
};

或者您将需要Thread.sleep(10000);在 Thread run 方法中移动,而不是在您传递给Handler.post方法的 Runnable 中:

Thread th = new Thread()
    {
        public void run() {
            /** call Thread.Sleep here... **/
            Thread.sleep(10000);
            handler.post(
            ....your code here....
于 2013-05-01T18:44:17.947 回答
0

是在主handler活动中定义的,因此它将在 UI 线程上运行。您要求睡眠 10 秒,这在 UI 线程上不起作用。

如果您想显示进度条并在 10 秒后将其关闭,请使用handler.postDelayed(Runnable r, long delayMillis).

这是代码:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // dialog = ProgressDialog.show(this,getString(R.string.loading) ,getString(R.string.alignMsg));
    dialog = ProgressDialog.show(this, "Loading", "Loading the Image of the Day");
    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            dialog.dismiss();
        }
    }, 10000);
}

希望对您有所帮助。

于 2013-05-01T18:45:15.447 回答