English 中文(简体)
Blackberry - How to make acountdown timer and show on Screen when we Point on the button
原标题:Blackberry - How to make a count down timer and display on screen when we click on the button

在我点击 but子时,我想显示点击时间。 如果我们点击纽顿,请送我显示时间的代码。

最佳回答

尝试这项法典

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;
}

}
问题回答

暂无回答




相关问题
How do I wait for a TTimer to finish?

I have a TFrame (fraDisplay) with a TTimer (timAnimateDataChange). The timer is used to control a small animation. In the form containing the frame I want to have a method that does something like ...

How to tell if an ajax timer has gone off at page_load

I have a web application and I m attempting to put an ajax timer control in it to give me a post back every 10-20 seconds. (possibly longer depending on performance). I have a lot of dynamically ...

Got an error 1120 ~ Access of undefined property

This is not your typical 1120. I know better than to have buttons/MCs without instance names on the timeline. Nope, this problem resides in a I timer I built from script I found online. The undefined ...

what is the right way to call the Timer ElapsedEventHandler

i have a timer that calls its even handler method every 30 seconds. but i want to initialy call this method. the signature of the event handler method is void TimerElapsed_GenerateRunTimes(object ...

Timer for a polling windows service

I wrote a Timer class in order to use it within a windows service that is polling another system. I did this because I had two issues that the System.Timers.Timer is not addressing. The Elapsed ...

Using C# Timer to stop executing a Program

I want a program to stop executing for a certain amount of time. And i want this to happen in regular intervals. For example, i want a program to run for 5 minutes and then it should stop for 2 ...

热门标签