Java 在安卓系统中,将错误登录尝试按钮延迟10秒,然后再次启用?
我有以下代码,它是一个登录应用程序重定向到一个网站。我找不到特定的在线帮助来禁用登录btn一段时间,然后在尝试返回到初始3的情况下再次启用。非常感谢您的帮助Java 在安卓系统中,将错误登录尝试按钮延迟10秒,然后再次启用?,java,android,Java,Android,我有以下代码,它是一个登录应用程序重定向到一个网站。我找不到特定的在线帮助来禁用登录btn一段时间,然后在尝试返回到初始3的情况下再次启用。非常感谢您的帮助 public class MainActivity extends AppCompatActivity { EditText username; EditText password; TextView attempt_count; Button login_btn; int attempts_remaining = 3; @Overri
public class MainActivity extends AppCompatActivity {
EditText username;
EditText password;
TextView attempt_count;
Button login_btn;
int attempts_remaining = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.editText_user);
password = (EditText) findViewById(R.id.editText_password);
attempt_count = (TextView) findViewById(R.id.textView_attempts_count);
login_btn = (Button) findViewById(R.id.button);
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (username.getText().toString().equals("admin") && password.getText().toString().equals("password")) {
Toast.makeText(getApplicationContext(), "Redirecting ...", Toast.LENGTH_SHORT).show();
Intent website = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.uk"));
startActivity(website);
} else {
Toast.makeText(getApplicationContext(), "Wrong Username or Password", Toast.LENGTH_SHORT).show();
attempt_count.setVisibility(View.VISIBLE);
attempt_count.setTextColor(Color.RED);
attempts_remaining--;
attempt_count.setText(Integer.toString(attempts_remaining));
if (attempts_remaining == 0) {
Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Please Wait", Toast.LENGTH_SHORT).show();
login_btn.setEnabled(false);
}
}
}
});
}
}您可以在禁用代码下面添加一段简单的代码
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
login_btn.setEnabled(true);
attempts_remaining = 3;
}
},10000);
您可以在禁用代码下面添加一段简单的代码
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
login_btn.setEnabled(true);
attempts_remaining = 3;
}
},10000);
基于此,您可以尝试以下操作:
if (attempts_remaining == 0) {
Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Please Wait", Toast.LENGTH_SHORT).show();
login_btn.setEnabled(false);
login_btn.postDelayed(new Runnable() {
@Override
public void run() {
login_btn.setEnabled(true);
attempts_remaining = 3;
}
}, 10 * 1000); // 10 seconds
}
基于此,您可以尝试以下操作:
if (attempts_remaining == 0) {
Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Please Wait", Toast.LENGTH_SHORT).show();
login_btn.setEnabled(false);
login_btn.postDelayed(new Runnable() {
@Override
public void run() {
login_btn.setEnabled(true);
attempts_remaining = 3;
}
}, 10 * 1000); // 10 seconds
}
你可以试试这个
你需要的是一个训练员,
从Android的开发者页面
处理程序允许您发送和处理与线程MessageQueue关联的消息和可运行对象。每个处理程序实例都与一个线程和该线程的消息队列相关联。当您创建一个新的处理程序时,它被绑定到正在创建它的线程的线程/消息队列——从那时起,它将向该消息队列传递消息和可运行文件,并在它们从消息队列中出来时执行它们
处理程序有两个主要用途:(1)将消息和可运行程序安排为将来某个时间点执行;以及(2)将要在不同于您自己的线程上执行的操作排队
将onClick方法更改为
@Override
public void onClick(View v) {
if (username.getText().toString().equals("admin") && password.getText().toString().equals("password")) {
Toast.makeText(getApplicationContext(), "Redirecting ...", Toast.LENGTH_SHORT).show();
Intent website = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.uk"));
startActivity(website);
} else {
Toast.makeText(getApplicationContext(), "Wrong Username or Password", Toast.LENGTH_SHORT).show();
attempt_count.setVisibility(View.VISIBLE);
attempt_count.setTextColor(Color.RED);
attempts_remaining--;
attempt_count.setText(Integer.toString(attempts_remaining));
if (attempts_remaining == 0) {
Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Please Wait", Toast.LENGTH_SHORT).show();
login_btn.setEnabled(false);
new Handler().postDelayed(new Runnable()
{
@Override //depending on if you are inheriting from a super class
public void run()
{
login_btn.setEnabled(true);
}
}, 10000 // time in milliseconds, 10000ms = 10s
);
}
}
}
});
}
}
您可能需要进行一些调整以适应您的代码,请告诉我这是怎么回事。您可以试试这个
你需要的是一个训练员,
从Android的开发者页面
处理程序允许您发送和处理与线程MessageQueue关联的消息和可运行对象。每个处理程序实例都与一个线程和该线程的消息队列相关联。当您创建一个新的处理程序时,它被绑定到正在创建它的线程的线程/消息队列——从那时起,它将向该消息队列传递消息和可运行文件,并在它们从消息队列中出来时执行它们
处理程序有两个主要用途:(1)将消息和可运行程序安排为将来某个时间点执行;以及(2)将要在不同于您自己的线程上执行的操作排队
将onClick方法更改为
@Override
public void onClick(View v) {
if (username.getText().toString().equals("admin") && password.getText().toString().equals("password")) {
Toast.makeText(getApplicationContext(), "Redirecting ...", Toast.LENGTH_SHORT).show();
Intent website = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.uk"));
startActivity(website);
} else {
Toast.makeText(getApplicationContext(), "Wrong Username or Password", Toast.LENGTH_SHORT).show();
attempt_count.setVisibility(View.VISIBLE);
attempt_count.setTextColor(Color.RED);
attempts_remaining--;
attempt_count.setText(Integer.toString(attempts_remaining));
if (attempts_remaining == 0) {
Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Please Wait", Toast.LENGTH_SHORT).show();
login_btn.setEnabled(false);
new Handler().postDelayed(new Runnable()
{
@Override //depending on if you are inheriting from a super class
public void run()
{
login_btn.setEnabled(true);
}
}, 10000 // time in milliseconds, 10000ms = 10s
);
}
}
}
});
}
}
你可能需要做一些调整来适应你的代码,让我知道这是怎么回事