How in android easier and better to know the number of milliseconds between the first and second pressing the same button?

  • Closure (closures, lambda expressions) can be implemented with this. The first time you click ( lastClicked == null variable) you write the current time ( lastClicked = timestamp ) into this variable. The next time you press, you draw the difference between the current time and lastClicked . - Sultanov
  • For timestamp use this one? System.CurrentTimeMillis. - androman
  • I can’t help with the code, I’m not kodil on java and don’t know all the details In the internet they write that CurrentTimeMillis cannot give an exact value and produces different results on calls on different processes (somewhere it is always a multiple of 16ms, somewhere is 1ms). There are still other functions .. in general experimenting) - Sultanov Shamil
  • I would be accurate to within 50 milliseconds. And then let it be rounded. - androman

1 answer 1

Something like this:

 private long lastClickMillis=0L; Button button = (Button)findViewById(R.id.myButton); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { long thisClickMillis=System.currentTimeMillis(); Log.i("Timer", "Time elapsed since last click="+(thisClickMillis-lastClickMillis) lastClickMillis=thisClickMillis; } }); 

PS I'm good in the morning, the weather is probably good ...

  • global variables ... choose the dangerous path :) - Sultanov Shamil
  • @SultanovShamil and what is dangerous here? - pavel
  • This is not a global variable :), but a class variable. In addition, you probably do not know that lambda expressions are just a shortened form of writing anonymous classes, in which only local variables are available (whose values ​​are not stored), final variables (which cannot be changed), and class members. - Barmaley
  • a class variable that another method of the same class can easily change ..) how does it work on java — I don’t really care, I don’t write there)) and the closure came to us from functional languages, and the current problem can be solved with their help more elegant and safer imho - Sultanov Shamil
  • @SultanovShamil - code in the studio (in any language), otherwise it’s just bla-bla - Barmaley