[英]how to prevent execution of same thing again and again
我已经做了一个应用程序,每当用户输入特定单词时,它现在就烤一个按摩,我想添加一个功能,如果用户在特定时间内连续多次输入特定单词,它将只考虑一次并烤按摩只有那些。
编码
if ( string.equals("help") ) {
Toast.makeText(getApplicationContext(), "we are here to help", Toast.LENGTH_SHORT).show();
}
使用全局字符串变量让我们将其命名为lastText
并检查输入文本是否与最后一个文本相同。
private String lastText = ""; // Global for all class members
//...
// May be more code goes here
//...
if ( string.equals("help") && !string.equals(lastText) ) {
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;
}
您可以将已键入的单词添加到列表中,当用户键入单词时,您可以遍历此列表并检查是否已经键入了“word”,然后如果“false” - 显示 toast。
替代解决方案 - 您可以创建一个本地 SQLite 数据库(使用 Room 持久性库),即使用户重新启动/退出应用程序,它也允许您保存值,但它需要更多编码和修复细微差别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.