是否有可能一个计时器添加到Android的动作条? [英] Is it possible to add a timer to the actionbar on android?
问题描述
我想一个简单的MM:SS定时器要在我的动作条显示,能够选择的分钟数,开始从倒计时,然后调用一个方法,一旦它为0。这是可能的,如何?谢谢
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 at 0. Is this possible and how? Thank you
推荐答案
我已经成功地做到这一点通过添加的TextView
的操作栏为的动作视图菜单,然后定期用更新它 CountDownTimer
。
(测试在Android 3.0模拟器,在Android 4.3模拟器和真实设备运行的是Android 4.3)。
I've managed to do that by adding a TextView
to the Action Bar as an Action View in the menu, then updating it at regular intervals using a CountDownTimer
.
(Tested on an Android 3.0 emulator, an Android 4.3 emulator and a real device running Android 4.3).
下面是XML的菜单项。需要注意的是 actionViewClass =android.widget.TextVew
(showAsAction和actionViewClass千万不要因为我使用的支援行动吧拥有了Android命名空间)。
Here's the XML for the menu item. Note that actionViewClass="android.widget.TextVew"
(showAsAction and actionViewClass don't have the android namespace because I'm using the support action bar).
<?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
,我找到菜单项
和呼叫 getActionView
将其分配给私有的TextView
, timerText
。我也适用于一些填充在这里,所以它是不正确的,在屏幕的边缘。
In onCreateOptionsMenu
, I find the MenuItem
and call getActionView
to assign it to private TextView
, timerText
. I also apply some padding here, so that it's not right at the edge of the screen.
@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
不访问它已被初始化之前。 (previously,我叫 startTimer
从的onCreate
,却得到了一个 NullPointerException异常
)。
I have a method called startTimer
to start a CountDownTimer
and update timerText
in its onTick
. I call startTimer
from onCreateOptionsMenu
to ensure that timerText
is not accessed before it has been initialised. (Previously, I called startTimer
from onCreate
, but got a 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();
}
我敢肯定,任何人读这就会知道一个更好的方式来秒的整数转换为一个时间code,如00:00:00,但以防万一我给大家介绍的 secondsToString
方法,我用 onTick
,上面。
I'm sure that anyone reading this will know a better way to convert an integer number of seconds to a timecode such as "00:00:00", but just in case I'll share the secondsToString
method I used in onTick
, above.
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;
}
这篇关于是否有可能一个计时器添加到Android的动作条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!