Created
November 12, 2009 16:39
-
-
Save shaobin0604/233043 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.yo2.aquarium.android.testtimer; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.TextView; | |
public class TestTimer extends Activity { | |
private Button btnStart; | |
private Button btnStop; | |
private Button btnReset; | |
private TextView tvCounter; | |
private long count = 0; | |
private boolean run = false; | |
private Handler handler = new Handler(); | |
private Runnable task = new Runnable() { | |
public void run() { | |
// TODO Auto-generated method stub | |
if (run) { | |
handler.postDelayed(this, 1000); | |
count++; | |
} | |
tvCounter.setText("Count: " + count); | |
} | |
}; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
btnStart = (Button) findViewById(R.id.Button01); | |
btnStop = (Button) findViewById(R.id.Button02); | |
btnReset = (Button) findViewById(R.id.Button03); | |
tvCounter = (TextView) findViewById(R.id.counter); | |
btnStart.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
run = true; | |
updateButton(); | |
handler.postDelayed(task, 1000); | |
} | |
}); | |
btnStop.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
run = false; | |
updateButton(); | |
handler.post(task); | |
} | |
}); | |
btnReset.setOnClickListener(new OnClickListener() { | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
count = 0; | |
run = false; | |
updateButton(); | |
handler.post(task); | |
} | |
}); | |
} | |
private void updateButton() { | |
btnStart.setEnabled(!run); | |
btnStop.setEnabled(run); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment