我做了一个应用程序,每当用户输入一个特定的单词,它就会进行按摩,现在我想添加一个功能,如果用户在特定的时间连续多次输入特定的单词,它将只考虑它一次,只为按摩的那个干杯。
密码
if ( string.equals("help") ) {
Toast.makeText(getApplicationContext(), "we are here to help", Toast.LENGTH_SHORT).show();
}发布于 2022-08-31 06:20:46
使用全局字符串变量,让我们将其命名为lastText,并检查输入文本是否与最后一个文本相同。
时间跟踪更新
private String lastText = ""; // Global for all class members
private long lastTextTime = 0; // Global
//...
// May be more code goes here
//...
if ( string.equals("help") && !string.equals(lastText) ) {
// Check whether 5 min has elapsed to show the toast message
if(System.currentTimeMillis - lastTextTime > (5*60*1000)) {
Toast.makeText(getApplicationContext(), "we are here to help",
Toast.LENGTH_SHORT).show();
// If the program reach here save this string as a last text for the next check
lastText = string;
lastTextTime = System.currentTimeMillis;
}
}发布于 2022-08-31 06:25:36
您可以将已经键入的单词添加到列表中,当用户键入一个单词时,您可以遍历这个列表并检查" word“是否已经键入,然后检查"false”是否已键入,然后显示“false”。
替代解决方案--您可以创建一个本地SQLite数据库(使用库),它将允许您保存值,即使用户重新启动/退出应用程序,但它需要更多的编码和修复细微之处
https://stackoverflow.com/questions/73551447
复制相似问题