[英]How do I disable a button temporarily even when the app is close?
So I created a simple login activity. 因此,我创建了一个简单的登录活动。 The login button will be disabled for a period of time when the user failed to login 3 times.
当用户登录3次失败时,登录按钮将被禁用一段时间。
My struggle is when I close the app and open it again the button is enabled back. 我的挣扎是当我关闭应用程序并再次打开它时,该按钮又被启用了。 How to fix this?
如何解决这个问题?
Here's my code: 这是我的代码:
public class LoginControl extends Activity {
private DBControl db = new DBControl(this);
int counter = 2;
Button login = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginview);
Button register = (Button) findViewById(R.id.btnCreateA);
login = (Button) findViewById(R.id.btnLogin);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
EditText a = (EditText) findViewById(R.id.etUser);
EditText b = (EditText) findViewById(R.id.etPassword);
String user = a.getText().toString();
String pass = b.getText().toString();
String confirm = db.getUserPass(user);
if (user.equals("") || pass.equals("")) {
Toast passed = Toast.makeText(LoginControl.this, "Please input required fields.", Toast.LENGTH_LONG);
passed.show();
} else if (pass.equals(confirm)) {
Toast passed = Toast.makeText(LoginControl.this, "Sucess!", Toast.LENGTH_LONG);
passed.show();
Intent intent = new Intent(LoginControl.this, HomeControl.class).putExtra("Music", false);
startActivity(intent);
finish();
} else if (counter == 0)
// Disable button after 3 failed attempts
{
login.setEnabled(false);
Toast alert = Toast.makeText(LoginControl.this, "Login Disabled for 5 mins", Toast.LENGTH_LONG);
alert.show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
login.setEnabled(true);
counter = 2;
}
}, 30000);
} else {
Toast passed = Toast.makeText(LoginControl.this, "Username or password don't match!", Toast.LENGTH_LONG);
counter--;
passed.show();
}
} catch (Exception e) {
Toast passed = Toast.makeText(LoginControl.this, e.toString(), Toast.LENGTH_LONG);
passed.show();
}
}
});
}
} }
To get this information, even if the app is restarted, you need to save the time when the button will be enabled again. 要获取此信息,即使重新启动应用程序,也需要节省再次启用该按钮的时间。 When your app starts you can open this information and check if it is before or after this time.
应用启动时,您可以打开此信息,并检查它是否在此时间之前或之后。
There are several methods, how you can store information on android: 有几种方法,如何在android上存储信息:
For your problem I would suggest that you should use Shared Preferences. 对于您的问题,我建议您使用共享首选项。
Using shared preferences, save the state of your button after login.setEnabled(false)
: 使用共享首选项,在
login.setEnabled(false)
之后保存按钮的状态:
SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
boolean enabled = login.isEnabled();
prefs.edit().putBoolean("LOGIN_ENABLED_KEY", enabled).apply();
Just after getting hold of the login
button in onCreate
, check for the presence of this value (using true
as the default value): 紧握
onCreate
中的login
按钮之后,检查是否存在此值(使用true
作为默认值):
SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
boolean enabled = prefs.getBoolean("LOGIN_ENABLED_KEY", true);
login.setEnabled(enabled);
If the button is disabled at this point, you need to restart the timer so that it eventually gets enabled: 如果此时已禁用该按钮,则需要重新启动计时器,以便最终启用它:
if (!enabled) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
login.setEnabled(true);
prefs.edit().clear().apply();
counter = 2;
}
}, 30000);
}
When the timer elapses: 计时器过去后:
SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
prefs.edit().putBoolean("LOGIN_ENABLED_KEY", true).apply();
Alternatively, just clear the shared preferences: 或者,只需清除共享的首选项:
SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
prefs.edit().clear().apply();
Putting it all together, something roughly like this: 放在一起,大概是这样的:
int counter = 2;
Button login = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginview);
final SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
login = (Button) findViewById(R.id.btnLogin);
boolean enabled = prefs.getBoolean("LOGIN_ENABLED_KEY", true);
login.setEnabled(enabled);
if (!enabled) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
login.setEnabled(true);
prefs.edit().clear().apply();
counter = 2;
}
}, 30000);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
EditText a = (EditText) findViewById(R.id.etUser);
EditText b = (EditText) findViewById(R.id.etPassword);
String user = a.getText().toString();
String pass = b.getText().toString();
if (user.equals("") || pass.equals("")) {
Toast passed = Toast.makeText(LoginControl.this, "Please input required fields.", Toast.LENGTH_LONG);
passed.show();
} else if (pass.equals("pass")) {
Toast passed = Toast.makeText(LoginControl.this, "Success!", Toast.LENGTH_LONG);
passed.show();
// Start HomeControl + finish()
} else if (counter == 0) {
// Disable button after 3 failed attempts
login.setEnabled(false);
prefs.edit().putBoolean("LOGIN_ENABLED_KEY", false).apply();
Toast alert = Toast.makeText(LoginControl.this, "Login Disabled for 5 mins", Toast.LENGTH_LONG);
alert.show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
login.setEnabled(true);
prefs.edit().clear().apply();
counter = 2;
}
}, 30000);
} else {
Toast passed = Toast.makeText(LoginControl.this, "Username or password don't match!", Toast.LENGTH_LONG);
counter--;
passed.show();
}
} catch (Exception e) {
Toast passed = Toast.makeText(LoginControl.this, e.toString(), Toast.LENGTH_LONG);
passed.show();
}
}
});
}
(There is clearly room for some refactoring here, but this does work.) (显然这里有一些重构的空间,但这确实可行。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.