我对编程android应用程序还很陌生,因此需要一些帮助。很抱歉,这个问题很长!
我当前正在尝试编写一个计算次数的应用程序,有人按下按钮。 该计数器应该每天重置一次(这是一种工作)。但目前,它会在第一次点击时显示前一天的点击次数,并且仅从第二次点击开始算起。 有人可以告诉我我哪里出错了吗?
我还希望在另一个活动中显示点击数,在该活动中,我创建了一个包含2小时计时器的处理程序(出于测试目的,我只放置了10秒)。如果2个小时未单击按钮,则会显示AlertDialog,提醒用户单击按钮。 当前,这种方式的工作方式是处理程序将计时器倒计时并显示对话框,但前提是用户没有离开活动。如果未打开活动,则计时器结束后,将不会显示任何对话框。此外,处理程序不会在再次单击该按钮时重置,而是启动另一个计时器。 当活动当前未打开时,我如何才能使计时器在每次单击按钮时重置并启动并显示对话框?
谢谢大家!
我主要活动中的相应代码:
Button btnWater;
int counter;
private String day;
String today;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Abfrage des aktuellen Datums:
Calendar calendar = Calendar.getInstance();
today = DateFormat.getDateInstance().format(calendar.getTime());
Button btnWater = (Button) findViewById(R.id.btnWater);
btnWater.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (day.equals(today)) {
counter++;
if (counter <= 8) {
Intent waterIntent = new Intent(getApplicationContext(), WaterActivity.class);
startActivity(waterIntent);
} else {
Toast.makeText(getApplicationContext(), "Prima! Sie haben schon über 2 Liter getrunken! Gut gemacht!", Toast.LENGTH_LONG).show();
Intent waterIntent = new Intent(getApplicationContext(), WaterActivity.class);
startActivity(waterIntent);
}
} else {
counter = 1;
Intent waterIntent = new Intent(getApplicationContext(), WaterActivity.class);
startActivity(waterIntent);
day = today;
}
}
});
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
day = prefs.getString("zaehlerTag", "08.11.2018");
counter = prefs.getInt("zaehler", 0);
}
//Sicherstellen, dass Timer "weiterzählt", wenn App geschlossen wird!
@Override
protected void onStop() {
super.onStop();
SharedPreferences prefs = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("zaehler", counter);
editor.putString("zaehlerTag", day);
editor.apply();
}
和我的WaterActivity(带有显示计数器的处理程序的那个):
int zaehler;
private Handler handler;
private long timeRemaining = 10000;
TextView tvWasserCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water);
//Anlegen der Textelemente:
TextView tvWasser1 = (TextView) findViewById(R.id.tvWasser1);
TextView tvWasserCount = (TextView) findViewById(R.id.tvWasserCount);
TextView tvWasser2 = (TextView) findViewById(R.id.tvWasser2);
//Anlegen des Handlers als Timer für die Wassererinnerung:
handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
Log.d("timer", "klappt");
timeRemaining = timeRemaining - 1000;
if (timeRemaining > 0) {
handler.postDelayed(this, 1000);
} else if (timeRemaining == 0) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH");
String strTime = mdformat.format(calendar.getTime());
int time = Integer.parseInt(strTime);
if (time >= 6 && time <= 22) {
AlertDialog.Builder builder = new AlertDialog.Builder(WaterActivity.this);
builder.setMessage("Sie haben schon seit 2 Stunden nichts getrunken! Jetzt wäre ein guter Zeitpunkt!").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog aDialog = builder.create();
aDialog.show();
}
}
}
};
handler.postDelayed(runnable, 1000);
//SharedPreferences zum Abrufen des aktuellen Zähler-Stands:
SharedPreferences prefs = getSharedPreferences("prefs", 0);
zaehler = prefs.getInt("zaehler", 1);
if (zaehler <= 1) {
tvWasserCount.setText(zaehler + " Glas");
} else {
tvWasserCount.setText(zaehler + " Gläser");
}