【问题标题】:today date only once toast message show今天仅约会一次吐司消息显示
【发布时间】:2014-08-05 06:13:22
【问题描述】:

我正在实现如果今天日期存在则仅显示一次警报消息的概念。

示例 5-8-14 仅显示一次 toast 消息。

6-8-14 只显示一次 toast 消息 ' 在每个日期只有一次敬酒表演。

编辑: 只有在第一次启动 App 后,才会出现带有当前日期的 toast。如果我第二次或第三次启动我的应用程序,那么应该不会出现 toast

【问题讨论】:

  • 模糊的问题。请更清楚并发布一些代码。
  • 展示你的逻辑,你为此做了什么?
  • 每天一次 Toast 消息秀。我打开我的应用程序很多次,但今天第一次只约会一次 toast 秀。明天我打开应用程序第一次 Toast 秀

标签: android sharedpreferences toast


【解决方案1】:

逻辑:

什么:
1. 当您展示吐司时,将日期/日期保存在 sharedPreferences 中。
2. 然后每次将“今天的日期”的值与 sharedPref 的值进行比较,如果不同,则显示 toast。

如何:
- 为 toast 创建一个函数,在其中更新 Shared Pref 值。
- 在“if”循环中,比较默认的共享首选项值——所以当你第一次运行应用程序时,它会返回默认值并进入 show toast 函数。

例如

if(!(sharedPrefSavedDate.equals(sharedPrefDefaultValue))){
if(!(String.valueOf(new SimpleDateFormat("dd").format(new java.util.Date())).equals(sharedPrefSavedDate)){
                             showToast();
}
}  

showToast() 中,将今天日期的值保存在共享首选项中。

保存的值可以是整数或字符串。可以相应地将.equals 更改为!==

可能需要:
How to use shared preferences

Example with integer on android developers

【讨论】:

    【解决方案2】:

    查看 sharedPreferences。 这里是安卓开发者的链接:sharedPreferences

    在第一次启动后设置一个布尔值,每次启动后检查该值,如果为真,则不显示 toast

    获取日期:

    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("dd:MMMM:yyyy");
    String strDate = sdf.format(c.getTime());
    

    链接:Get current time and date

    写:

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putBool("Toast", True);
    editor.putString("Date", strDate);
    editor.commit();
    

    阅读:

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    bool bToast = sharedPref.getBool("Toast", False);
    String strDate = sharedPref.getString("Date", null);
    

    【讨论】:

    • 布尔值何时为假?要求是需要检查日期,而不是布尔值:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2020-01-03
    • 2020-03-06
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多