按下暂停按钮后我的秒表重置为零,我的代码可能有什么问题?
My stopwatch resets to zero after pause button is pressed, what could be wrong in my code?
我创建了一个秒表应用程序,在测试它时我注意到一个问题,即按下暂停按钮后秒表会重置为零。
(当用户在暂停按钮后按下开始时,秒表从零开始)。
这是我的代码:
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:
我刚刚添加了一个等于零的长变量 "until pause" 并计算了从用户按下按钮开始和暂停按钮那一刻起的时间差,它解决了这个问题。
您没有 Pause
按钮。您只需将 Start
按钮的文本更改为 "Pause"。因此,每次单击 "Pause" 时,您实际上只是再次单击 Start
。如果你真的想要 "Pause" 它,你要么需要放入一个真正的 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()
应该将 startTime
设置回0.
这不准确。只是一个关于你应该如何处理它的指南。它不需要非常复杂 - 事实上,如果是,你可能做错了。越简单越好!
我看不到 oldReceiver
变量的定义位置及其类型。
也许会发生一些转换,结果为零。
好的做法是将 属性 名称定义为 "constant"。
最好检查按钮标签以确定是暂停还是开始点击。
我创建了一个秒表应用程序,在测试它时我注意到一个问题,即按下暂停按钮后秒表会重置为零。 (当用户在暂停按钮后按下开始时,秒表从零开始)。 这是我的代码:
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: 我刚刚添加了一个等于零的长变量 "until pause" 并计算了从用户按下按钮开始和暂停按钮那一刻起的时间差,它解决了这个问题。
您没有 Pause
按钮。您只需将 Start
按钮的文本更改为 "Pause"。因此,每次单击 "Pause" 时,您实际上只是再次单击 Start
。如果你真的想要 "Pause" 它,你要么需要放入一个真正的 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()
应该将 startTime
设置回0.
这不准确。只是一个关于你应该如何处理它的指南。它不需要非常复杂 - 事实上,如果是,你可能做错了。越简单越好!
我看不到 oldReceiver
变量的定义位置及其类型。
也许会发生一些转换,结果为零。
好的做法是将 属性 名称定义为 "constant"。
最好检查按钮标签以确定是暂停还是开始点击。