倒计时吧Android示例 [英] countdown bar android example

查看:90
本文介绍了倒计时吧Android示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何帮助,以显示进度条上这个简单的倒计时?

any help to display this simple countdown on progress bar?

new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }


 }.start();

http://developer.android.com/reference/android/os/ CountDownTimer.html

推荐答案

您可以尝试这种替代方法,我已经创造了它,当我在学习进度条,

You can try this alternate way, I have created it when i was learning about progress bar,

main.xml中,

Main.xml,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="@string/Button02Text" android:id="@+id/Button02"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
    </Button>
</LinearLayout>

进度活动,

public class ProgressBarExampleActivity extends Activity 
{
    ProgressThread progThread;
    ProgressDialog progDialog;
    Button button1, button2;
    int typeBar;                     // Determines type progress bar: 0 = spinner, 1 = horizontal
    int delay = 1000;                  // Milliseconds of delay in the update loop
    int maxBarValue = 30;           // Maximum value of horizontal progress bar

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Process button to start horizontal progress bar dialog with anonymous inner class
        button2 = (Button) findViewById(R.id.Button02);
        button2.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                typeBar = 1;
                showDialog(typeBar);
            }
        }); 
    }

    // Method to create a progress bar dialog of either spinner or horizontal type
    @Override
    protected Dialog onCreateDialog(int id) 
    {
        switch(id) 
        {
        case 1:                      // Horizontal
            progDialog = new ProgressDialog(this);
            progDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progDialog.setMax(maxBarValue);
            progDialog.setMessage("Dollars in checking account:");
            progThread = new ProgressThread(handler);
            progThread.start();
            return progDialog;
        default:
            return null;
        }
    }

    // Handler on the main (UI) thread that will receive messages from the 
    // second thread and update the progress.

    final Handler handler = new Handler() 
    {
        public void handleMessage(Message msg) 
        {
            // Get the current value of the variable total from the message data
            // and update the progress bar.
            int total = msg.getData().getInt("total");
            progDialog.setProgress(total);
//          if (total >= maxBarValue)
            if (total <= 0 )            
            {
                dismissDialog(typeBar);
                progThread.setState(ProgressThread.DONE);
            }
        }
    };

    // Inner class that performs progress calculations on a second thread.  Implement
    // the thread by subclassing Thread and overriding its run() method.  Also provide
    // a setState(state) method to stop the thread gracefully.

    private class ProgressThread extends Thread 
    {   
        // Class constants defining state of the thread
        final static int DONE = 0;
        final static int RUNNING = 1;

        Handler mHandler;
        int mState;
        int total;

        // Constructor with an argument that specifies Handler on main thread
        // to which messages will be sent by this thread.

        ProgressThread(Handler h) 
        {
            mHandler = h;
        }

        // Override the run() method that will be invoked automatically when 
        // the Thread starts.  Do the work required to update the progress bar on this
        // thread but send a message to the Handler on the main UI thread to actually
        // change the visual representation of the progress. In this example we count
        // the index total down to zero, so the horizontal progress bar will start full and
        // count down.

        @Override
        public void run() 
        {
            mState = RUNNING;   
            total = maxBarValue;
            while (mState == RUNNING) 
            {
                // The method Thread.sleep throws an InterruptedException if Thread.interrupt() 
                // were to be issued while thread is sleeping; the exception must be caught.
                try 
                {
                    // Control speed of update (but precision of delay not guaranteed)
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    Log.e("ERROR", "Thread was Interrupted");
                }

                // Send message (with current value of  total as data) to Handler on UI thread
                // so that it can update the progress bar.

                Message msg = mHandler.obtainMessage();
                Bundle b = new Bundle();
                b.putInt("total", total);
                msg.setData(b);
                mHandler.sendMessage(msg);

                total--;    // Count down
            }
        }

        // Set current state of thread (use state=ProgressThread.DONE to stop thread)
        public void setState(int state) 
        {
            mState = state;
        }
    }
}

这篇关于倒计时吧Android示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆