Android应用程序中的CountUp时间

时间:2014-04-25 16:06:47

标签: java android

我正在尝试为Android设备创建一个Age计算器应用程序,现在我在这个应用程序中获得了出生日期和当前日期,然后我得到了那些时间的差异,并将其显示给用户。

问题是我正在寻找一种为Age计算器添加countUp功能的方法,该计算器计算用户年龄的计时器(LIKE LIVE AGING:D)。

这是我的年龄计算器类:

public class AgeCalculation {
    private int startYear;
    private int startMonth;
    private int startDay;
    private int endYear;
    private int endMonth;
    private int endDay;
    private int resYear;
    private int resMonth;
    private int resDay;
    private Calendar start;
    private Calendar end;
    public String getCurrentDate()
    {
          end=Calendar.getInstance();
          endYear=end.get(Calendar.YEAR);
          endMonth=end.get(Calendar.MONTH);
          endMonth++;
          endDay=end.get(Calendar.DAY_OF_MONTH);
          return endDay+":"+endMonth+":"+endYear;
    }
    public void setDateOfBirth(int sYear, int sMonth, int sDay)
    {
     startYear=sYear;
     startMonth=sMonth;
     startMonth++;
     startDay=sDay;

    }
    public void calcualteYear()
    {
        resYear=endYear-startYear;

    }

    public void calcualteMonth()
    {
        if(endMonth>=startMonth)
        {
             resMonth= endMonth-startMonth;
        }
        else
        {
            resMonth=endMonth-startMonth;
            resMonth=12+resMonth;
            resYear--;
        }

    }
    public void  calcualteDay()
    {

        if(endDay>=startDay)
        {
             resDay= endDay-startDay;
        }
        else
        {
            resDay=endDay-startDay;
            resDay=30+resDay;
            if(resMonth==0)
            {
                resMonth=11;
                resYear--;
            }
            else
            {
                resMonth--;
            }

        }
    }

    public String getResult()
    {
        return  resYear + " year  " + resMonth + " month  " + resDay + " day " ;
    }
     public long getSeconde()
     {
         start=Calendar.getInstance();
         start.set(Calendar.YEAR, startYear);
         start.set(Calendar.MONTH, startMonth);
         start.set(Calendar.DAY_OF_MONTH, startDay);
         start.set(Calendar.HOUR, 12);
         start.set(Calendar.MINUTE, 30);
         start.set(Calendar.SECOND, 30);
         start.set(Calendar.MILLISECOND, 30);
         long now=end.getTimeInMillis();
         long old=start.getTimeInMillis();
         long diff=old-now;
         return diff/1000;
     }
}

1 个答案:

答案 0 :(得分:1)

创建一个计时器,每秒(或其他)递增一个值,然后在文本字段中显示。有点像:

public void ageTimer() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                liveAge++
            }
        }, 0, 1000);
    }

然后你可以将它附加到年,日等。添加你自己的检查,当它达到60秒,然后增加分钟等。

然后您可以在文本字段中显示它:

yourAge = (TextView) findViewById(R.id.yourAge); // where yourAge is the id of your textview in xml
yourAge.setText(liveAge);