Android - Update TextView periodically

前端 未结 2 504
灰色年华
灰色年华 2021-01-22 14:36

I simply want to update a TextView periodically by a service. Let\'s say the TextView should display the time and update itself every 30 seconds. Could\'t you guys tell me how I

2条回答
  •  长发绾君心
    2021-01-22 14:59

    Handler h = new Handler();
    int delay = 30000; //milliseconds
    
    h.postDelayed(new Runnable(){
        public void run(){
            editText.setText(java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime())+"");
            h.postDelayed(this, delay);
        }
    }, delay);
    

提交回复
热议问题