问题描述
我正在创建一个简单的烹饪应用程序。所需的行为如下:
用户按下 PRE-HEAT 和 chronoGeneral 和 chronopreHeat 开始。 用户按 COOKING 和 chronopreHeat 停止。 用户按下 OFF-HEAT,chronoCooking 停止。
此时,我希望,如果用户按下 offheatResetBtn,chronoOffheat 将重置为 00:00:00,并将 chronoGeneral 的值恢复到 chronoOffheat 启动之前。我遇到的问题是,尽管在我再次启动 chronoGeneral 时使用它在启动 chronoOffheat 之前的值将文本设置为 chronoGeneral,但它开始从更高的值而不是我之前设置的值开始计数。
我尝试调整基值,但没有奏效。
package com.robin.ovenmaster;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.time.temporal.ChronoUnit;
public class ChronoFragment extends Fragment {
static String startingPointText,lastGeneralText,lastPreheatText,lastCookingText,lastOffheatText;
static Chronometer chronoGeneral,chronopreHeat,chronoCooking,chronoOffheat;
Button generalResetBtn,preheatStartBtn,preheatResetBtn,cookingStartBtn,cookingResetBtn,offheatStartButton,offheatResetBtn,foodreadyBtn;
TextView generalText,preheatText,cookingText,offheatText;
boolean btnPreHeatClicked,btnCookingClicked,btnOffheatClicked;
boolean isChronoGeneralRun,isChronopreHeatRun,isChronoCookingRun,isChronoOffheatRun;
public static ChronoFragment newInstance() {
return new ChronoFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_chrono,container,false);
}
@Override
public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
super.onViewCreated(view,savedInstanceState);
/**
* Initialize Elements
*/
startingPointText = "00:00:00";
lastGeneralText = "00:00:00";
lastPreheatText = "00:00:00";
lastCookingText = "00:00:00";
lastOffheatText = "00:00:00";
chronoGeneral = view.findViewById(R.id.simpleChronometer0);
chronopreHeat = view.findViewById(R.id.simpleChronometer1);
chronoCooking = view.findViewById(R.id.simpleChronometer2);
chronoOffheat = view.findViewById(R.id.offheat_chronometer);
preheatStartBtn = view.findViewById(R.id.preheat_start_btn);
cookingStartBtn = view.findViewById(R.id.button1);
offheatStartButton = view.findViewById(R.id.offheatStartBtn);
foodreadyBtn = view.findViewById(R.id.buttonReady);
generalResetBtn = view.findViewById(R.id.general_reset_btn);
preheatResetBtn = view.findViewById(R.id.preheat_reset_btn);
cookingResetBtn = view.findViewById(R.id.cooking_reset_btn);
offheatResetBtn = view.findViewById(R.id.offheatResetBtn);
generalText = view.findViewById(R.id.general_text);
preheatText = view.findViewById(R.id.preheat_text);
cookingText = view.findViewById(R.id.cooking_text);
offheatText = view.findViewById(R.id.offheat_text);
/**
* Initialize Elements
*/
chronoGeneral.setonChronometerTickListener(new ChronoListener());
chronoGeneral.setBase(SystemClock.elapsedRealtime());
chronoGeneral.setText(lastCookingText);
chronopreHeat.setonChronometerTickListener(new ChronoListener());
chronopreHeat.setBase(SystemClock.elapsedRealtime());
chronopreHeat.setText(lastPreheatText);
chronoCooking.setonChronometerTickListener(new ChronoListener());
chronoCooking.setBase(SystemClock.elapsedRealtime());
chronoCooking.setText(lastCookingText);
chronoOffheat.setonChronometerTickListener(new ChronoListener());
chronoOffheat.setBase(SystemClock.elapsedRealtime());
chronoOffheat.setText(lastOffheatText);
/**
* Start Listeners
*/
preheatStartBtn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!btnPreHeatClicked) {
setLastTextValues();
btnPreHeatClicked = true;
chronoGeneral.setBase(SystemClock.elapsedRealtime());
chronopreHeat.setBase(SystemClock.elapsedRealtime());
chronoGeneral.start();
isChronoGeneralRun = true;
chronopreHeat.start();
isChronopreHeatRun = true;
}
}
});
cookingStartBtn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!btnCookingClicked && !chronoGeneral.getText().equals(startingPointText) && !chronopreHeat.getText().equals(startingPointText)) {
setLastTextValues();
btnCookingClicked = true;
chronopreHeat.stop();
chronoCooking.setBase(SystemClock.elapsedRealtime());
chronoCooking.start();
if (!isChronoGeneralRun) {
chronoGeneral.setBase(SystemClock.elapsedRealtime() - (chronoCooking.getBase() - chronoGeneral.getBase()));
//chronoGeneral.setText(lastGeneralText);
chronoGeneral.start();
}
}
}
});
offheatStartButton.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!btnOffheatClicked && !chronoGeneral.getText().equals(startingPointText) && !chronopreHeat.getText().equals(startingPointText) && !chronoCooking.getText().equals(startingPointText)) {
setLastTextValues();
btnOffheatClicked = true;
chronoGeneral.start();
chronoCooking.stop();
chronoOffheat.setBase(SystemClock.elapsedRealtime());
chronoOffheat.start();
}
}
});
/**
* Reset Listeners
*/
generalResetBtn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resetAllChronos();
}
});
preheatResetBtn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resetAllChronos();
}
});
cookingResetBtn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!chronoCooking.getText().equals(startingPointText)) {
chronoGeneral.stop();
chronoGeneral.setText(lastGeneralText); // Reset the time to before running 'cookingStartBtn'.
isChronoGeneralRun = false;
chronopreHeat.stop();
chronopreHeat.setText(lastPreheatText);
isChronopreHeatRun = false;
chronoCooking.stop();
chronoCooking.setText("00:00:00");
isChronoCookingRun = false;
chronoOffheat.stop();
chronoOffheat.setText("00:00:00");
isChronoOffheatRun = false;
btnCookingClicked = false; // Allow the button to be clickable again
}
}
});
offheatResetBtn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!chronoOffheat.getText().equals(startingPointText)) {
//chronoGeneral.setBase(chronoGeneral.getBase() + (SystemClock.elapsedRealtime() - chronoOffheat.getBase()));
chronoGeneral.stop();
chronoGeneral.setText(lastGeneralText);
isChronoGeneralRun = false;
//chronoOffheat.setBase(SystemClock.elapsedRealtime());
chronoOffheat.stop();
chronoOffheat.setText("00:00:00");
isChronoOffheatRun = false;
btnOffheatClicked = false; //Allow the button to be clickable again
}
}
});
/**
* Food Ready Listener
*/
foodreadyBtn.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chronoGeneral.stop();
chronoOffheat.stop();
generalText.setText(chronoGeneral.getText());
preheatText.setText(chronopreHeat.getText());
cookingText.setText(chronoCooking.getText());
offheatText.setText(chronoOffheat.getText());
}
});
}
void resetAllChronos() {
chronoGeneral.setBase(SystemClock.elapsedRealtime());
chronoGeneral.stop();
chronopreHeat.setBase(SystemClock.elapsedRealtime());
chronopreHeat.stop();
chronoCooking.setBase(SystemClock.elapsedRealtime());
chronoCooking.stop();
chronoOffheat.setBase(SystemClock.elapsedRealtime());
chronoOffheat.stop();
chronoGeneral.setText("00:00:00");
chronopreHeat.setText("00:00:00");
chronoCooking.setText("00:00:00");
chronoOffheat.setText("00:00:00");
btnPreHeatClicked = false;
btnCookingClicked = false;
btnOffheatClicked = false;
}
class ChronoListener implements Chronometer.OnChronometerTickListener {
@Override
public void onChronometerTick(Chronometer chronometer) {
long time = SystemClock.elapsedRealtime() - chronometer.getBase();
int h = (int) (time / 3600000);
int m = (int) (time - h * 3600000) / 60000;
int s = (int) (time - h * 3600000 - m * 60000) / 1000;
String t = (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
chronometer.setText(t);
}
}
void setLastTextValues() {
lastGeneralText = chronoGeneral.getText().toString();
lastPreheatText = chronopreHeat.getText().toString();
lastCookingText = chronoCooking.getText().toString();
lastOffheatText = chronoOffheat.getText().toString();
}
}
按下 RESET 之前(OFF-HEAT)
按下 RESET (OFF-HEAT) 后
按下 OFF-HEAT 后
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)