How in android easier and better to know the number of milliseconds between the first and second pressing the same button?
1 answer
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
|
lastClicked == nullvariable) you write the current time (lastClicked = timestamp) into this variable. The next time you press, you draw the difference between the current time andlastClicked. - Sultanov