java - pass a variable to an onClick method -


so want calculate total amount of time occurs when activity created , when button pressed. can current code, uses static variables know bad programming practice there way me this.

my current code:

static long starttime=0;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_quick_touch);         // show button in action bar.         setupactionbar();         starttime=system.currenttimemillis(); }  public void onclick(view view)      {         long endtime = system.currenttimemillis();         long time= endtime- starttime;       } 

you don't need use static variable- non-static member variable work. however, if didn't want cluttering activity @ make private inner class onclicklistener, , have store time in member variable passed in via constructor.


Comments

Popular posts from this blog

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

Android Gson.fromJson error -