简体   繁体   English

即使应用程序关闭,如何暂时禁用按钮?

[英]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.

相关问题 如何临时禁用/绕过Android OnTouchEvent - How do I temporarily disable/bypass the Android OnTouchEvent 如何禁用应用内设置按钮 - How do i disable the in-app settings button 我想通过在当前应用程序中按一个按钮来终止/关闭特定应用程序。 我怎样才能做到这一点? - I want to kill/ close a particular app by pressing a button in my current app. How can I do this? 如何在Java GWT服务器端临时关闭带缓冲的读取器? - How do i temporarily close a buffered reader in java gwt server side? 如何暂时禁用EGit? - How to disable EGit temporarily? 当我在应用浏览器中的android webview中打开链接时,如何在左上方放置关闭按钮 - when i open a link in android webview in app browser how can i put close button on top left 即使我关闭了 .netbeans,你如何在 JavaDB 中“启动服务器”? - How do you "start server" in JavaDB even if I close the netbeans? 如何解决? 单击该按钮后,我的应用程序将强制关闭 - How to fix it?? My app getting force close when I click the button 当用户尝试关闭窗口按钮时,如何检查文件是否已保存? - How do I check if a file is saved when the user tries to close the window button? 关闭应用程序时如何断开与设备的连接? - How do I disconnect from a device when I close my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM