Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

8.6. TimerTask 更新 UI

		
	Timer timer = new Timer();
		
	private void Clock() {

        TextView textViewTime = findViewById(R.id.textViewTime);
        TimerTask task = new TimerTask() {
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

            public void run() {
                String current = dateFormat.format(new Date());
                textViewTime.post(new Runnable() {
                    @Override
                    public void run() {
                        textViewTime.setText(current);
                    }
                });
                Log.d(TAG, current);
            }
        };
        timer.schedule(task, 1000 * 5, 1000);
//        timer.cancel();
    }