12

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

4

3 回答 3

16

我已经设法做到这一点,方法是将 a 添加TextView到操作栏作为菜单中的操作视图,然后使用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将其分配给 private 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. 我调用startTimerfromonCreateOptionsMenu以确保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;
}
于 2014-01-08T15:35:22.533 回答
3

菜单xml

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

**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;

 }
于 2016-01-20T07:26:45.250 回答
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 :)

于 2018-02-07T01:27:34.727 回答