Android中用Handle做定时器,显示实时时间

用Handle做定时器,显示时间,实现详见代码:


...

        mTimeTv = new TextView(this);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        String date = df.format(new java.util.Date());
        mTimeTv.setText(date);
        mTimeTv.setTextSize(35);
        mTimeTv.setTextColor(Color.BLACK);

        timeHandler.postDelayed(updateTimeThread, 1000);
    }
    Handler timeHandler = new Handler();
    Runnable updateTimeThread = new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            String date = df.format(new java.util.Date());
            mTimeTv.setText(date);
            timeHandler.postDelayed(updateTimeThread, 1000);
        }
    };

你可能感兴趣的:(Android)