1

现在,这是我在 android 中的第一步。我只是想用“下载”按钮来实现进度条。当我按下下载按钮时,进度条继续进行,但是当整个进度结束时,我无法隐藏进度条。这是我的代码。请帮我。

public class ProgressBarDemo extends Activity
{

    ProgressBar pb;
    Button bt;

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

        pb = (ProgressBar) findViewById(R.id.progressBar1);
        bt = (Button) findViewById(R.id.button1);

        pb.setVisibility(View.VISIBLE);

        bt.setOnClickListener(new OnClickListener() 
        {

            public void onClick(View v)
            {
                Thread timer = new Thread()
                {
                    public void run()
                    {
                        try
                        {
                            for(int i=0; i<=50; i ++)
                            {   
                                pb.incrementProgressBy(i);
                                sleep(1000);
                            }

                                pb.setVisibility(View.INVISIBLE);
                                Toast.makeText(ProgressBarDemo.this, "Thank you for downloading", Toast.LENGTH_SHORT).show();

                        }catch(Exception e){}

                    }
                };
                timer.start();
            }

        });

    }

}
4

3 回答 3

1

您应该只修改 mainThread(UI 线程)中的 ui 元素尝试

pb.post(new Runnable() {

        @Override
        public void run() {
            pb.setVisibility(View.INVISIBLE);
        }
    })

反而。

也许你必须使用相同的东西来增加你的进度条。或者,您可以使用 AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html类。onProgressUpdate 和 onPostExecute 在 UIThread 中自动调用。

于 2012-08-22T09:52:55.013 回答
0

你不应该从不同的线程做 UI 任务。使用这个......

public class ProgressBarDemo extends Activity
    {

        ProgressBar pb;
        Button bt;
   @Override
        protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    pb = (ProgressBar) findViewById(R.id.progressBar1);
    bt = (Button) findViewById(R.id.button1);

    pb.setVisibility(View.VISIBLE);

    bt.setOnClickListener(new OnClickListener() 
    {

        public void onClick(View v)
        {
            Thread timer = new Thread()
            {
                public void run()
                {
                    try
                    {
                        for(int i=1; i<=100; i ++)
                        {   
                            pb.setProgress(i);
                            sleep(100);
                        }


                                }catch(Exception e){}
                    finally{
                        runOnUiThread( new Runnable() {
                            public void run() {
                                 pb.setVisibility(View.INVISIBLE);
                            Toast.makeText(ProgressBarDemo .this, "Thank you for downloading", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                }
            };
            timer.start();
        }

    });

}


    }
于 2012-08-22T09:59:44.760 回答
0

你也可以使用 AsynTask 这是一个更好的解决方案......

public class ProgressBarDemo extends Activity
{

    ProgressBar pb;
    Button bt;
@Override
    protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

pb = (ProgressBar) findViewById(R.id.progressBar1);
bt = (Button) findViewById(R.id.button1);

pb.setVisibility(View.VISIBLE);

bt.setOnClickListener(new OnClickListener() 
{

    public void onClick(View v)
    {
        new AsynTasks().execute();
    }
});

}

class AsynTasks extends AsyncTask<Void, Integer, Void>
{

    @Override
    protected Void doInBackground(Void... params) {
        for(int i=1;i<=100;i++)
        {
            SystemClock.sleep(1000);
             publishProgress(i);
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
          pb.setVisibility(View.INVISIBLE);
          Toast.makeText(ProgressBarDemo .this, "Thank you for downloading", Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
          pb.setProgress(values[0]);
    }
}
}
于 2012-08-22T11:12:44.763 回答