Android - Update TextView periodically

前端 未结 2 475
灰色年华
灰色年华 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);
    
    0 讨论(0)
  • 2021-01-22 15:02

    In onCreate() //updating every 30 seconds

          Handler handler =new Handler();
          final Runnable r = new Runnable() {
                    public void run() {
                        handler.postDelayed(this, 30000);
                        String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
                        edittextId.setText(mydate);
                    }
                };
                handler.postDelayed(r, 0000);
    
    0 讨论(0)
提交回复
热议问题