스톱워치 응용 프로그램을 만들었는데 테스트했을 때 일시 중지 버튼을 누른 후 중지 시계가 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"를 추가하고 사용자가 버튼 시작과 일시 정지 버튼을 누른 순간부터 시간 차이를 계산하여 문제를 해결했습니다.
아무도 알아낼 수없는 부분이 있습니까? –
이것을 읽으십시오. http://stackoverflow.com/help/mcve – nerdlyist