按下暂停按钮后,我的秒表重置为零,我的代码可能出错?

时间:2017-01-04 17:49:21

标签: android handler runnable

我创建了一个秒表应用程序,当我测试它时,我注意到一个问题,即在按下暂停按钮后秒表重置为零。 (当用户在暂停按钮后按下开始时,秒表从零开始)。 这是我的代码:

TextView tv;
EditText laps_editText;
Button btnstart, btnreset;
int a, b, c, d, e, f;
String splString, diffString;
long split, diff, firstsplit = 0;
int t = 1;
long startTime = 0;
long timeInMilis = 0;
long timeSwap = 0;
long updatedtime = 0;
int sec = 0, min = 0, hr = 0;
int secs = 0, mins = 0, hrs = 0;
int secspl = 0, minspl = 0, hrspl =0;
Handler handler = new Handler();
boolean isRunning = false;
String spl, dif;
int counter = 1;
ScrollView scrollView;
Button changeColor;
int selectedColor, savedColor;
SharedPreferences backGroundColor = null;
Context context;
RelativeLayout rLayout;
int n = 0;
SharedPreferences current = null;
long oldReciver;

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.fragment_stop_watch, container, false);
        laps_editText = (EditText) v.findViewById(R.id.lap_edittext);

        rLayout = (RelativeLayout) v.findViewById(R.id.r_layout);
        context = getActivity().getApplicationContext();
        tv = (TextView) v.findViewById(R.id.textView2);



        btnstart = (Button) v.findViewById(R.id.button3);
        btnreset = (Button) v.findViewById(R.id.button4);

        btnstart.setTypeface(custom_font);
        btnreset.setTypeface(custom_font);
        changeColor = (Button) v.findViewById(R.id.changeColor);

        // As you see I tried to use sharedpreferences to save the updatedtime variable that was before the user pressed the pause button
        current = context.getSharedPreferences("currentMil", Context.MODE_PRIVATE);

        savedColor = backGroundColor.getInt("color", 38536);
        rLayout.setBackgroundColor(savedColor);



        btnstart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// if the t == 1 the button will start the stopwath else it will stop it
                    if(t == 1){
                            oldReciver = current.getLong("updated", 00);
                            isRunning = true;
                            btnstart.setText("Pause");
                            btnreset.setText("Lap");
                            startTime = SystemClock.uptimeMillis() + oldReciver;
                            t = 0;
                            handler.postDelayed(updateTimer, 0);


                }
                else{
                    isRunning = false;
                    btnstart.setText("Start");
                    btnreset.setText("Reset");
                    timeSwap += timeInMilis;
                    handler.removeCallbacks(updateTimer);
                    n = 1;
                    t = 1;
                }
            }
        });
        btnreset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /*  if(isRunning){
                    split = SystemClock.uptimeMillis() - startTime;
                    secspl = (int)(split / 1000);
                    minspl = sec / 60;
                    hrspl = min / 60;
                    secspl = sec % 60;

                    if (firstsplit == 0){
                        diff = split;
                        secs = (int)(diff / 1000);
                        mins = sec / 60;
                        hrs = min / 60;
                        secs = sec % 60;
                        firstsplit = split;
                    }
                    else{
                        diff = split - firstsplit;
                        secs = (int)(diff / 1000);
                        mins = sec / 60;
                        hrs= min / 60;
                        secs = sec % 60;
                        firstsplit = split;
                    }
                    dif = String.valueOf(hrs) + ":" + String.valueOf(mins) + ":" + String.valueOf(secs);
                    spl = String.valueOf(hrspl) + ":" + String.valueOf(minspl) + ":" + String.valueOf(secspl);




                }
ignore those lines above they are just for laps 
*/
                    else{
                        startTime = 0;
                        t = 1;
                        timeInMilis = 0;
                        timeSwap = 0;
                        sec = 0;
                        min = 0;
                        hr = 0;
                        updatedtime = 0;
                        laps_editText.setText("");
                        btnstart.setText("Start");
                        handler.removeCallbacks(updateTimer);
                        tv.setText("00:00:00");
                        n = 1;
                        counter = 1;
                    }
                }
                public String getDiff(){
                    return dif;
                }
                public String getSpl(){
                    return spl;
                }

            });


            return v;

        }

 public Runnable updateTimer = new Runnable() {
        @Override
        public void run() {
            timeInMilis = SystemClock.uptimeMillis() - startTime;
            updatedtime = timeSwap + timeInMilis;
// here i,m tring to save updatetime value
            SharedPreferences.Editor editor = current.edit();
            editor.putLong("updated", updatedtime);
            editor.commit();
            sec = (int)(updatedtime / 1000);
            min = sec / 60;
            hr = min / 60;
            sec = sec % 60;
            tv.setText(String.format("%02d", hr) + ":" + String.format("%02d", min) + ":" + String.format("%02d", sec)  );
            handler.postDelayed(this, 0);
        }
    };

任何帮助?!

编辑1: 我刚刚添加了一个长变量"直到暂停"等于零,并且从用户按下按钮开始和暂停按钮的时刻开始计算时间差,并解决了问题。

2 个答案:

答案 0 :(得分:1)

我看不到定义oldReceiver变量的位置以及它的类型。 也许某些铸造会导致零。

好的做法是将属性名称定义为“常量”。

最好检查按钮标签以确定它是暂停还是开始点击。

答案 1 :(得分:0)

您没有Pause按钮。您只需将Start按钮的文字更改为"暂停"。因此,每次点击"暂停"时,您实际上只是再次点击Start。如果你想真正"暂停"它,您需要输入一个真实的Pause按钮,或者在点击按钮时检查按钮的内容:

long currTime = 0;
if (btnStart.Text == "Start")
{
    if (timeStart == 0;)
        timer.Start();
    else if (timeStart > 0)
        timeStart = currTime;
}
else if (btnStart.Text == "Pause")
{
    timer.Pause();
    currTime = timeStart;
}

我无法从此处看到您的代码,但您应该Start()增加startTimeReset()应设置startTime回到0.

这并不准确。只是一个指导你如何接近它的指南。它并不需要过于复杂 - 事实上,如果是这样,你可能做错了。越简单越好!