开发者

Android Development: seekbar count down!

Is it possible, if yes how, how do i go from progress 100 to 1 without the user doing anything.

Like for every .05sec s开发者_运维技巧eekbar.setProgress(-=1) So without the user doing anything the seekbar will go down until it reach 1.

Please anser how to do this

Thank-you


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

    ProgressDialog dialog;

    public Async(Context ctx) {
        dialog = new ProgressDialog(ctx);
        dialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub

        dialog.incrementProgressBy(1);

    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    dialog.dismiss();
    super.onPostExecute(result);
}


@Override
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
    int i=0;
    while (i < 1000) {

        publishProgress(1);
        i++;
    }
    return null;
}
}


You may be forced to write your own progress bar. Here's an example: http://techdroid.kbeanie.com/2010/04/custom-progressbar-for-android.html

There's more than one way to do it...


Something roughly like this (note: may not compile):

private int progressStatus = 100;

new Thread(new Runnable() 
        {
            public void run() 
            {
                while (progressStatus > 1) 
                {
                    progressStatus = doSomeWork();

                    //---Update the progress bar---
                    handler.post(new Runnable() 
                    {
                        public void run() {
                            progressBar.setProgress(progressStatus);
                        }
                    });
                }

                handler.post(new Runnable() 
                {
                    public void run() {
                        // Do your thing here when it's done
                    }
                });
            }    

            private int doSomeWork() 
            {
                try 
                {
                    //---simulate doing some work---
                    Thread.sleep(50);

                } catch (InterruptedException e) 
                {
                   e.printStackTrace();
                }

                int progress = progressStatus - 1;

                return progress;
            }

        }).start();  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜