再拍类计时 [英] Make another class to count time

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

问题描述

我有创建一个新类来计算时间的问题。这是我的code:

I have a problem with create a new class to count time. This is my code:

Button btcheck = (Button)findViewById(R.id.btcheck);

btcheck.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v){
        Time aa = new Time();
        aa.RunTime();                   
    }           
});

public class Time extends MainActivity {

    public void RunTime() {     
        startTime = SystemClock.uptimeMillis();
        customHandler.postDelayed(updateTimerThread, 0);            
    }

    public Runnable updateTimerThread = new Runnable() {
        public void run() {

            updatedTime = SystemClock.uptimeMillis() - startTime;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);      

            stime="" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds);                            

            txttime.setText(stime + " ");
            customHandler.postDelayed(this, 0);     
    };
}

然而,它一点儿也不工作,因为我期望的那样。希望大家帮我。

However it does'nt work as I expect. Hope you help me.

推荐答案

您可以使用扩展的AsyncTask代替MainActivity。你不创建一个新的活动这种方式回来日志,你可以在不与你的线程冲突更新你的用户界面。

You could use extends AsyncTask instead of MainActivity. This way you're not creating a new activity back log and you can update your UI without conflicting with your threading.

private Handler handler = new Handler( );
    public long startTime;
    public void onClick(View v){
        switch( v.getId( ) ){
            case R.id.btcheck:
                this.startTime = SystemClock.uptimeMillis();
                startTimer( );
                break;
        }
    }
    private void startTimer( ){
        startRunnable( );
    }

    private Runnable startRunnable( )
    {
        new Runnable( )
        {
            @Override
            public void run( )
            {
                new Time( startTime ).execute( );
                long timer = 1000;
                handler.postDelayed( startRunnable( ), timer );
            }
        };
        return null;
    }

    class Time extends AsyncTask< String, String, String >
    {
        private long startTime;
        private long updatedTime;

        public Time( long startTime )
        {
            this.startTime = startTime;
        }

        @Override
        protected void onPreExecute( )
        {
        }

        @Override
        protected void onPostExecute( String result )
        {
        }

        @Override
        protected String doInBackground( String... param )
        {
            updatedTime = SystemClock.uptimeMillis( ) - startTime;

            int secs = ( int ) ( updatedTime / 1000 );
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = ( int ) ( updatedTime % 1000 );

            publishProgress( "" + mins + ":" + String.format( "%02d", secs ) + ":" + String.format( "%03d", milliseconds ) );
            return null;
        }

        @Override
        protected void onProgressUpdate( String... values )
        {
            txttime.setText( values[ 0 ] + " " );
        }
    }

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

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