2

当我尝试使用 setText 方法在 textView 上显示计时器时,它正在工作。但是这里的计时器(文本)没有得到任何更新,卡上只显示“0, 0, 0, 0”。我正在使用cardlib 库来显示卡片。我不知道是否可以在卡片上显示计时器。谁能帮我克服这个问题?

*这是我的代码:

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

抱歉,由于我的声誉,我无法上传图片。这是我的卡片图片的链接:

http://tinypic.com/view.php?pic=a2vdx5&s=8#.VeShM_btmko

4

3 回答 3

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()调用这个方法。

于 2015-08-31T19:00:10.073 回答
1

我通过在 cardlib 中使用 setupInnerViewElements 方法解决了我的问题。我将 CountDownTimer 类放入此方法中,并且有效。这是我的java代码:

    public MyCard1(Context context) {
            super(context, R.layout.home_card_inner_content1);
    }

    @Override
    public void setupInnerViewElements(ViewGroup parent, View view) {

        TextView mTitleTextView1 = (TextView) parent.findViewById(R.id.home_card_main_inner_title1);
        final TextView mSecondaryTitleTextView1 = (TextView) parent.findViewById(R.id.home_card_main_inner_subtitle1);

            new CountDownTimer(86500000, 1000) { // adjust the milli seconds here

                public void onTick(long millisUntilFinished) {

                    day1 = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
                    hour1 = TimeUnit.MILLISECONDS.toHours(millisUntilFinished)- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millisUntilFinished));
                    min1 = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished));
                    sec1 = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished));
                    mSecondaryTitleTextView1.setText(day1 + " day, " + hour1 + " hour, " + min1 + " minute, " + sec1 + " second");
                }
                public void onFinish() {mSecondaryTitleTextView1.setText("done!");}
            }.start();

        if (mTitleTextView1 != null)
            mTitleTextView1.setText(mainTitle1);

        if (mSecondaryTitleTextView1 != null)
            mSecondaryTitleTextView1.setText(secondaryTitle1);
    }
}
于 2015-09-01T12:29:58.240 回答
0

每次处理(我的意思是,向用户展示)都必须在 Java 中的线程中完成(因此在 Android 上)。然后你需要使用一个 RUNNABLE,不要使用 TIMER TASKS(android 上的定时器任务有点错误)。创建一个属性:

 private final Handler handler2 = new Handler();//As attribute e.g.

制作了一个可运行的(作为属性):

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

要启动此可运行文件,您必须在需要时调用它

handler1.postDelayed(runnable1, 100);

要停止此可运行文件,您必须在需要时调用它:

handler1.removeCallbacks(runnable1);
于 2015-08-31T19:08:10.470 回答