Is it possible to add a timer to the actionbar on android?

后端 未结 3 1893
说谎
说谎 2021-02-08 02:29

I\'d like a simple mm:ss timer to be displayed on my actionbar, be able to pick the number of minutes to start counting down from and then call a method once it\'s

3条回答
  •  庸人自扰
    2021-02-08 02:47

    Menu xml

    
    
    

    **public boolean onCreateOptionsMenu(Menu menu)"

    long timer = 10000;
    public Boolean onCreateOptionsMenu(Menu menu) 
    {
            super.onCreateOptionsMenu(menu);
            getMenuInflater().inflate(R.menu.menu, menu);
    
           final MenuItem  counter = menu.findItem(R.id.counter);
            new CountDownTimer(timer, 1000) {
    
              public void onTick(long millisUntilFinished) {
                 long millis = millisUntilFinished; 
                 String  hms = (TimeUnit.MILLISECONDS.toHours(millis))+":"+(TimeUnit.MILLISECONDS.toMinutes(millis) -TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)))+":"+ (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 
    
                 counter.setTitle(hms);
                 timer = millis;
    
                }
    
            public void onFinish() {
                 counter.setTitle("done!");
                }
             }.start();
    
             return  true;
    
     }
    

提交回复
热议问题