Tuesday, May 1, 2012
Display Stop Watch (Timer) in Blackberry
import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public class stopwatch extends MainScreen
{
private RichTextField _chronometer;
Timer timer;
private int second = 0, minute = 0, hour = 0;
private boolean _sec = false, _min = false, _hou = false;
ButtonField record,stop;
public stopwatch()
{
record=new ButtonField("Start");
stop=new ButtonField("Stop");
_chronometer = new RichTextField("0" + hour + ":0" + minute + ":0" + second, RichTextField.TEXT_ALIGN_HCENTER | Field.NON_FOCUSABLE);
add(_chronometer);
HorizontalFieldManager hfm=new HorizontalFieldManager(FIELD_HCENTER);
record.setMargin(net.rim.device.api.system.Display.getHeight()/2,0,0,0);
stop.setMargin(net.rim.device.api.system.Display.getHeight()/2,0,0,20);
hfm.add(record);
hfm.add(stop);
add(hfm);
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if(field==record){
try
{
try {
timer = new Timer();
resetChronometer();
timer.scheduleAtFixedRate(new Chronometer(), 1000, 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
}
if(field==stop){
try
{
timer.cancel();
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
}
}
};
record.setChangeListener(listener);
stop.setChangeListener(listener);
}
private class Chronometer extends TimerTask {
public void run() {
try {
second++;
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if (_min == true) {
_min = false;
minute = minute + 1;
second = 0;
}
if (_hou == true) {
_hou = false;
hour = hour + 1;
minute = 0;
}
if (second == 59) {
_min = true;
if (minute == 59) {
_hou = true;
}
}
if (second <= 9 && minute <= 9 && hour <= 9) {
_chronometer.setText("0" + hour + ":0" + minute + ":0" + second);
}
if (second > 9 && minute <= 9 && hour <= 9) {
_chronometer.setText("0" + hour + ":0" + minute + ":" + second);
}
if (second <= 9 && minute > 9 && hour <= 9) {
_chronometer.setText("0" + hour + ":" + minute + ":0" + second);
}
if (second <= 9 && minute <= 9 && hour > 9) {
_chronometer.setText("" + hour + ":0" + minute + ":0" + second);
}
if (second > 9 && minute > 9 && hour > 9) {
_chronometer.setText(hour + ":" + minute + ":" + second);
}
if (second > 9 && minute > 9 && hour <= 9) {
_chronometer.setText("0" + hour + ":" + minute + ":" + second);
}
if (second > 9 && minute <= 9 && hour > 9) {
_chronometer.setText("" + hour + ":0" + minute + ":" + second);
}
if (second <= 9 && minute > 9 && hour > 9) {
_chronometer.setText("0" + hour + ":" + minute + ":" + second);
}
// rt.setText(hour+":"+minute+":"+second);
}
});
// timer.cancel();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void resetChronometer() {
_chronometer.setText("00:00:00");
second = 0;
minute = 0;
hour = 0;
}
}
Subscribe to:
Post Comments (Atom)
Really it used more effective development codes. It is the best stopwatch.
ReplyDelete