是否可以在Android上的操作栏中添加计时器?

时间:2013-07-02 16:09:53

标签: android-actionbar countdowntimer

我想要一个简单的mm:ss计时器显示在我的操作栏上,能够选择开始倒计时的分钟数,然后在方法为0时调用方法。这可能吗?如何?谢谢

3 个答案:

答案 0 :(得分:15)

我设法通过在操作栏中将TextView添加为菜单中的Action View,然后使用CountDownTimer定期更新。
> (在Android 3.0模拟器,Android 4.3模拟器和运行Android 4.3的真实设备上测试过。)

这是菜单项的XML。请注意actionViewClass="android.widget.TextVew"
(showAsAction和actionViewClass没有android命名空间,因为我正在使用支持操作栏)。

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:compatibility="http://schemas.android.com/apk/res-auto" >

    <item android:id="@+id/break_timer"
          android:title="@string/break_timer_title"
          compatibility:showAsAction="always"
          compatibility:actionViewClass="android.widget.TextView" />

</menu>

onCreateOptionsMenu中,我找到MenuItem并致电getActionView将其分配给私人TextViewtimerText。我也在这里应用了一些填充,因此它不在屏幕的边缘。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.break_timer, menu);

    MenuItem timerItem = menu.findItem(R.id.break_timer);
    timerText = (TextView) MenuItemCompat.getActionView(timerItem);

    timerText.setPadding(10, 0, 10, 0); //Or something like that...

    startTimer(30000, 1000); //One tick every second for 30 seconds
    return true;
}

我有一个名为startTimer的方法来启动CountDownTimer并在其timerText中更新onTick。我从startTimer致电onCreateOptionsMenu,以确保在初始化之前未访问timerText。 (之前,我从startTimer拨打了onCreate,但获得了NullPointerException

private void startTimer(long duration, long interval) {

    CountDownTimer timer = new CountDownTimer(duration, interval) {

        @Override
        public void onFinish() {
            //TODO Whatever's meant to happen when it finishes
        }

        @Override
        public void onTick(long millisecondsLeft) {
            int secondsLeft = (int) Math.round((millisecondsLeft / (double) 1000));
            timerText.setText(secondsToString(secondsLeft));
        }
    };

    timer.start();
}

我确信阅读此内容的任何人都知道将整数秒转换为时间码(例如“00:00:00”)的更好方法,但以防万一我将共享secondsToString我在上面的onTick中使用的方法。

private String secondsToString(int improperSeconds) {

    //Seconds must be fewer than are in a day

    Time secConverter = new Time();

    secConverter.hour = 0;
    secConverter.minute = 0;
    secConverter.second = 0;

    secConverter.second = improperSeconds;
    secConverter.normalize(true);

    String hours = String.valueOf(secConverter.hour);
    String minutes = String.valueOf(secConverter.minute);
    String seconds = String.valueOf(secConverter.second);

    if (seconds.length() < 2) {
        seconds = "0" + seconds;
    }
    if (minutes.length() < 2) {
        minutes = "0" + minutes;
    }
    if (hours.length() < 2) {
        hours = "0" + hours;
    }

    String timeString = hours + ":" + minutes + ":" + seconds;
    return timeString;
}

答案 1 :(得分:2)

菜单xml

<item
                android:id="@+id/counter"
                android:title=""
                android:showAsAction="always">
</item>

** public boolean onCreateOptionsMenu(菜单菜单)&#34;

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;

 }

答案 2 :(得分:0)

是的,可以在工具栏操作中添加计时器 在Java中使用此代码:

private void startTimer(long duration, long interval) {

CountDownTimer timer = new CountDownTimer(duration, interval) {

    @Override
    public void onFinish() {
        //TODO Whatever's meant to happen when it finishes
    }

    @Override
    public void onTick(long millisecondsLeft) {
        int secondsLeft = (int) Math.round((millisecondsLeft / (double) 1000));
        timerText.setText(secondsToString(secondsLeft));
    }
};

timer.start();
}

并且也使用 菜单文件定义的计时器ID就像这样

如果了解更多,请访问youtube:)