2017-01-04 1 views
0

스톱워치 응용 프로그램을 만들었는데 테스트했을 때 일시 중지 버튼을 누른 후 중지 시계가 0으로 재설정되는 문제가 있음을 알게되었습니다. (사용자가 버튼을 일시 중지 한 후 시작 버튼을 누르면 스톱워치가 0에서 시작됩니다). 다음은 내 코드입니다 :일시 중지 버튼을 누른 후 스톱 워치가 0으로 재설정됩니다. 내 코드에서 잘못되었을 수 있습니다.

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

어떤 도움을?!

EDIT 1 : 방금 ​​변수 "zero pause"를 추가하고 사용자가 버튼 시작과 일시 정지 버튼을 누른 순간부터 시간 차이를 계산하여 문제를 해결했습니다.

+0

아무도 알아낼 수없는 부분이 있습니까? –

+0

이것을 읽으십시오. http://stackoverflow.com/help/mcve – nerdlyist

답변

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

나는 당신을 볼 수 없습니다 ' 여기에서 코드를 다시,하지만 당신은 startTime를 증가 Start()을해야하고 Reset() 0

이 정확하지 다시 startTime을 설정해야합니다. 어떻게 접근해야하는지에 대한 안내서. 미친 듯이 복잡 할 필요는 없습니다. 사실 그렇다면 아마 잘못했을 것입니다. 더 단순합니다.

+0

하지만 "DO DO THIS"라고 선언 한 변수 t를 확인하고 그렇게합니까? –

+0

예. 전용 버튼을 가지지 않으려면 지정된 액션을 수행하기 위해 버튼의 내용을 확인해야합니다. 내 대답을 조금만 수정하면됩니다. –

1

oldReceiver 변수가 정의 된 위치와 그 유형을 볼 수 없습니다. 일부 캐스팅이 발생하여 결과가 0 일 수 있습니다.

"상수"로 속성 이름을 정의하는 것이 좋습니다.

단추 레이블을 확인하여 일시 중지 또는 클릭을 결정하는 것이 더 나을 수도 있습니다.

+0

oldReceiver가 긴 변수 인 경우 변수를 추가합니다 –

+0

'current = context.getSharedPreferences ("currentMil", Context.MODE_PRIVATE);'를 Runnable 정의에 추가하려고 할 수 있습니까? –

관련 문제