当我尝试使用setText方法在textView上显示计时器时,它正在运行。但这里的计时器(文本)没有得到任何更新,卡上只显示“0,0,0,0”。我正在使用cardslib库来显示卡片。我不知道是否可以在卡片上显示计时器。任何人都可以帮助我克服这个问题吗?
*这是我的代码:
long day, hour, min, sec;
MyCard card;
ArrayList<Card> cards = new ArrayList<Card>();
card = new MyCard(getActivity());
card.mainHeader= "Main Header";
card.mainTitle = "Main Title";
new CountDownTimer(86500000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
card.secondaryTitle = day + " day, " + hour + " hour, " + min + " minute, " + sec + " second";
day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished));
min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished));
sec = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished));
}
public void onFinish() {card.secondaryTitle = ("done!");}
}.start();
card.resourceIdThumb = R.drawable.ic_school_black_48dp;
CardHeader header = new CardHeader(getActivity());
header.setTitle(card.mainHeader);
//Add Header to card
card.addCardHeader(header);
//Add the thumbnail
CardThumbnail thumb = new CardThumbnail(getActivity());
thumb.setDrawableResource(card.resourceIdThumb);
card.addCardThumbnail(thumb);
card.setOnClickListener(listener);
cards.add(card);
抱歉,由于我的声誉,我无法上传图片。这是我卡片图片的链接:
答案 0 :(得分:1)
您可以创建一个每1秒更新一次的方法。您可以按照以下方式进行操作:
Thread t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// SetText here
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
创建一个更新卡片的方法,如下所示:
public void UpdateCard(){
card.secondaryTitle = day + " day, " + hour + " hour, " + min + " minute, " + sec + " second";
day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished));
min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished));
sec = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished));
}
在run()
内部调用此方法。
答案 1 :(得分:1)
我通过在cardslib中使用setupInnerViewElements方法解决了我的问题。我把CountDownTimer类放到这个方法中并且它起作用了。这是我的java代码:
>>> import re
>>> s = "<tr><th>Current Data Usage: </th><td>221.59GB</td></tr>"
>>> match = re.search(r"([\d\.]*)GB", s)
>>> match.group(1)
'221.59'
答案 2 :(得分:0)
每次处理(我的意思是,向用户显示)必须在Java中的线程中完成(因此在Android上)。然后你需要使用RUNNABLE,不要使用TIMER TASKS(android上的计时器任务是有点错误的)。创建一个属性:
private final Handler handler2 = new Handler();//As attribute e.g.
制作了一个runnable(作为属性):
private Runnable runnable1 = new Runnable() {
@Override
public void run() {
//made your edittext changes here
handler1.postDelayed(this, 100);//1000=1second, 100=1sec/10
//postDelayed = it is to recall the runnable
}
};
要启动此runnable,您必须在需要时调用它
handler1.postDelayed(runnable1, 100);
要停止此可运行,您必须在需要时调用它:
handler1.removeCallbacks(runnable1);