gpt4 book ai didi

java - 允许用户在 Android 中每天点击一次菜单

转载 作者:行者123 更新时间:2023-11-29 03:38:54 25 4
gpt4 key购买 nike

我正在创建一个在 actionbarsherlock 中有一个菜单项的程序。现在我希望用户每天单击该菜单一次,不再单击。我怎样才能做到这一点?这是我到目前为止所拥有的:

int hour = today.get(Calendar.HOUR_OF_DAY);

if (hour < 24) {

try {
if (gotGenders.contentEquals("Male")) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Monitor.this);
alertDialog.setTitle(gotNames);
alertDialog.setMessage("You are currently burning "
+ caloriesForMen() + " calories per hour");
alertDialog.setPositiveButton("Save",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub

}
});

alertDialog.setNegativeButton("Discard",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

alertDialog.setCancelable(false);
showAlertDialog = alertDialog.create();
showAlertDialog.show();
}
else if (gotGenders.contentEquals("Female")) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Monitor.this);
alertDialog.setTitle(gotNames);
alertDialog.setMessage("You are currently burning "
+ caloriesForWomen() + " calories per hour");
alertDialog.setPositiveButton("Save",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
// dialog.dismiss();
}
});

alertDialog.setNegativeButton("Discard",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

alertDialog.setCancelable(false);
showAlertDialog = alertDialog.create();
showAlertDialog.show();

count = 1;
}
} catch (Exception ex) {
ex.printStackTrace();

AlertDialog.Builder errorDialog = new AlertDialog.Builder(
Monitor.this);
errorDialog.setTitle("No User Selected!");
// errorDialog.setMessage("You are currently burning " +
// caloriesForWomen() + " per hour");
errorDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

errorDialog.setCancelable(false);
errorAlertDialog = errorDialog.create();
errorAlertDialog.show();
}
} else {
count = 0;

AlertDialog.Builder waitDialog = new AlertDialog.Builder(
Monitor.this);
waitDialog.setTitle("Wait after the day is over");
waitDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

waitDialog.setCancelable(false);
waitAlertDialog = waitDialog.create();
waitAlertDialog.show();
}

这允许我多次选择菜单,我如何限制它每天只能点击一次?感谢您提供任何帮助。

最佳答案

要实现这一点,您应该将上次访问日期存储在某处(数据库、共享首选项、文件中),然后:

long lastVistedDateTime = getLastVistedDateTime();
int lastDay = new Date(lastVistedDateTime).getDay();
int today = Calendar.get(Calendar.DATE);
if (today != lastDay){
//an other day
setLastVistedDateTime(Calendar.getInstance().getTimeInMillis());
}

要将日期与当前区域设置进行比较,请使用:

String lastDate = DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(lastVistedDateTime));
String nowDate = DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date());
if (!nowDate.equals(lastDate)){
//an other day
setLastVistedDateTime(Calendar.getInstance().getTimeInMillis());
}

关于java - 允许用户在 Android 中每天点击一次菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14089579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com