Java 倒计时和.setBackgroundColor()/.setTextColor()出现问题
我到处寻找答案,但找不到适合我情况的答案。我有几个问题,还想知道如何包括毫秒倒计时。我正在尝试获取格式为00.00(秒。毫秒)的倒计时计时器。一个按钮用于启动计时器。我使用的时间取决于按下的按钮,5、10、15、30或90秒。我只想说它的硬编码为5000毫秒,以使现在更简单Java 倒计时和.setBackgroundColor()/.setTextColor()出现问题,java,android,countdowntimer,Java,Android,Countdowntimer,我到处寻找答案,但找不到适合我情况的答案。我有几个问题,还想知道如何包括毫秒倒计时。我正在尝试获取格式为00.00(秒。毫秒)的倒计时计时器。一个按钮用于启动计时器。我使用的时间取决于按下的按钮,5、10、15、30或90秒。我只想说它的硬编码为5000毫秒,以使现在更简单 long timeSecs = 5000; // really timeSecs is dynamic but for the sake of simplicity long countDownInterval = 100
long timeSecs = 5000; // really timeSecs is dynamic but for the sake of simplicity
long countDownInterval = 1000; // this is a static value
TextView TVcountDown = (TextView)findViewById(R.id.TVcountDown);
public void createTimer() {
new CountDownTimer(timeSecs, countDownInterval) {
public void onTick(long millisUntilFinished) {
TVcountDown.setText(millisUntilFinished / 1000); // error here on
//.setText unless I cast to an int, which all values are long so I'm not sure why
}
@Override
public void onFinish() {
TVcountDown.setBackgroundColor(R.color.solid_red); // error here
TVcountDown.setTextColor(R.color.white); // error here
TVcountDown.setText("Expired"); // it will make it here
// It doesn't count down, just goes straight to onFinish() and displays "Expired"
}
}.start();
}
提前谢谢。我已经在桌子上捶了一会儿了。试试这个。
对于setText
TVcountDown.setText("" + (millisUntilFinished / 1000));
颜色
Resources res = getResources();
TVcountDown.setBackgroundColor(res.getcolor(R.color.solid_red));
TVcountDown.setTextColor(res.getcolor(R.color.white));
您应该在设置之前从颜色资源中获取颜色。谢谢!这一切都很好!但是,它仍然直接进入onFinish()并且实际上不倒计时。@Droidxxx将
countDownInterval
更改为10
(或更多),并使用mTextView.setText(((millisuntiltilfinished/10)/100.0)+”代码>查看秒和百分之一百毫秒。@Sam谢谢。也要感谢南迪希!还有一个问题。我试图保持格式00.00,所以在几秒钟内,我可以通过if(millisuntiltiffinished<10000){TVcountDown.setText(“0”+((millisuntiltiffinished/10)/100.0));}else TVcountDown.setText(“+((millisuntiltiffinished/10)/100.0));我如何跟踪.00以确保它是否为01.1直到显示01.10?查看SimpleDateFormat,new SimpleDateFormat(“HH:mm:ss”)
,并将两位数毫秒添加到上面。