首页 \ 问答 \ androidL如何重置计时器(androidL how to reset a timer)

androidL如何重置计时器(androidL how to reset a timer)

我正在学习使用计时器,并按照http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/中的示例进行操作。

我希望以一种方式实现,当用户按下按钮时计时器将启动,当用户的手关闭时停止,所以我编码如下:

代码:

button_right.setOnTouchListener( new View.OnTouchListener() 
{
    @Override
    public boolean onTouch(View arg0, MotionEvent event) 
    {
        if(event.getAction()==MotionEvent.ACTION_DOWN )
        {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);



        if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
        {
            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);
        }
        return false;
    }
});  



// setting timer
private Runnable updateTimerThread = new Runnable() 
{
    public void run() 
    {
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        tv_timing.setText("" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }
};      

问题:

一切正常,定时器将在用户按下按钮时启动,在保持时保持运行并在手动关闭时停止。 然而,我发现当用户再次按下按钮时,计时器从上次停止的位置开始,而不是在计算时间之前重置为0。

如果使用此代码,如何修改以使计时器重置为0,以便在再次按下按钮时重新计数? 谢谢!!


I am learning to use a timer, and follow the example in http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/.

I would like to implement in a way that the timer will start off when the user is pressing the button and stop when the user's hand is off, so I have coded as follows:

Codes:

button_right.setOnTouchListener( new View.OnTouchListener() 
{
    @Override
    public boolean onTouch(View arg0, MotionEvent event) 
    {
        if(event.getAction()==MotionEvent.ACTION_DOWN )
        {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);



        if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
        {
            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);
        }
        return false;
    }
});  



// setting timer
private Runnable updateTimerThread = new Runnable() 
{
    public void run() 
    {
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        tv_timing.setText("" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }
};      

Questions:

Everything works fine, and the timer will start when the user presses the button, keep running when holding and stop when hand is off. Yet I discover that when the user presses the button again, the timer start off from where it stops last time instead of resetting to 0 before counting time.

If using this code, how could it be modified such that the timer is reset to 0 so as to count all over again when the button is being pressed again? Thanks!!


原文:https://stackoverflow.com/questions/19211581
更新时间:2023-07-28 13:07

最满意答案

您尝试以错误的方式尝试操作创建帐户和注册日期,而不是仅为正常日期字段添加过滤器,并将这些字段放在工作表中并查看结果。

如果你真的想创建一个过滤器,那么你不能只使用普通过滤器来添加条件,而需要创建一个两个参数。

开始日期的一个参数

结束日期的一个参数

对于这两个参数使用日期字段来显示数据列表

现在创建两个calcualted字段

If create account date >=[start date parameter] and create account date < [end date parameter]
then 
your field
end

同样

If registration date >=[start date parameter] and registration date < [end date parameter]
then 
your field
end

使用行中的两个文件并将其放置在工作表的列中


You are trying in a wrong way don't try to manipulate create account and registration date instead add a filter only for normal date field and place the fields in sheet and see the result.

If you really want to create a filter then you can't add the condition just using normal filter instead you need to create a two parameters.

One parameter for start date

One parameter for end date

For both the parameters use date field to display the list of data

Now create two calcualted fields

If create account date >=[start date parameter] and create account date < [end date parameter]
then 
your field
end

Similarly

If registration date >=[start date parameter] and registration date < [end date parameter]
then 
your field
end

USe both the fileds in rows and place date in column of sheet

相关问答

更多
  • 不太确定这是否会有所帮助,但我遇到了类似的问题,并提出以下(+注释): 我认为你真的必须索引日期字段。 在查询/过滤等方面没有任何其他意义。 在Lucene.net v2.9中,与v2.9相比,有很多术语的范围查询似乎非常慢 通过切换到使用数字字段和数字字段查询,我在使用日期字段时修复了速度问题。 这实际上比我的Lucene.net v2.4基线提高了速度。 在缓存包装器过滤器中包装您的查询意味着您可以挂起为过滤器设置的文档位。 这也将大大加快使用相同过滤器的后续查询。 过滤器在一组查询结果的评分中不起作用 ...
  • 您可以通过使用dojo/store 查询过滤器实现此目的,使用回调函数进行精确查询以管理比较日期 并使用dojo/date compare功能将emplist数组的日期与特定日期进行比较,如下所示: this.employeeStore = new Memory({data: someData}); var compareDate = new Date(2014,1,1); var results = store.query(function(object){ // compare return ...
  • 您尝试以错误的方式尝试操作创建帐户和注册日期,而不是仅为正常日期字段添加过滤器,并将这些字段放在工作表中并查看结果。 如果你真的想创建一个过滤器,那么你不能只使用普通过滤器来添加条件,而需要创建一个两个参数。 开始日期的一个参数 结束日期的一个参数 对于这两个参数使用日期字段来显示数据列表 现在创建两个calcualted字段 If create account date >=[start date parameter] and create account date < [end date paramet ...
  • 唯一的方法是使用bool/should查询来捕获这个约束 要么没有AvailableUntil日期 或者AvailableUntil比今天晚 该查询将如下所示: var baseQuery = { "size": 1000, "query": { "bool": { "minimum_should_match": 1, "should": [ { "bool": { ...
  • 我通过https://www.datatables.net/examples/plug-ins/range_filtering.html获得了我的工作基地。 这是我的jsfiddle https://jsfiddle.net/bindrid/2bkbx2y3/6/ $(document).ready(function () { $.fn.dataTable.ext.search.push( function (settings, data, dataIndex) { ...
  • 要查找在另一个表中不匹配的行,可以使用LEFT JOIN/NULL模式。 使用LEFT JOIN ,子表上的条件将放入ON子句中。 父表上的条件放入WHERE子句中。 SELECT p.* FROM property_tbl AS p LEFT JOIN booking_tbl AS b ON p.id = b.propertyID AND booking_tbl.endDate > '17-10-24' AND booking_tbl.startDate < '17-10-31' WHERE b.prop ...
  • 我认为你不需要自定义搜索引擎。 您可以过滤交叉关联。 这应该适合你: filter :surveys_updated_at, label: 'By Date Completed', as: :date_range 你做的不是,一个洗劫者应该如何工作。 我知道这是一个常见的谷歌结果,但这是错误的。 在ActiveAdmin中,搜索者不会立即收到这两个日期。 搜索者将使用by_date_completed_gteq和by_date_completed_lteq 。 ransacker格式化程序仅用于格式化输入 ...
  • 要编辑,请使用dataTable语言扩展名。 在构造函数中添加此行。 "language": { "infoFiltered": "" }, 资源 In order to edit, use dataTable language extension. Add this line on your constructor. "language": { "infoFiltered": "" } ...
  • 我不明白为什么要反转数据表中的行。 从我的角度来看,这不是必要的。 您可以使用Linq查询过滤行,如下所示: private DataTable FilterTable(DataTable table, DateTime startDate, DateTime endDate) { var filteredRows = from row in table.Rows.OfType() where (DateTime) row[0] >= startDa ...
  • 我也遇到了这个任务,经过几个小时的尝试终于解决了它:)。 您需要使用媒体商店并查询它从日期和日期传递的时间长,这将返回一个位图数组以下是适合我的代码。 final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.Media.DATE_TAKEN, MediaStore ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)