我正在尝试使用正确的用户名和密码进入另一个活动(已创建)的简单登录页面此外,尝试计数器无法正常工作。对不起,如果这个措辞不好,希望这是有道理的。Android Studio - 登录页面无法正常工作
public class login extends Activity {
private EditText username;
private EditText password;
private Button login;
private TextView loginLocked;
private TextView attemptsLeft;
private TextView numberOfRemainingLoginAttemptsTV;
int numberOfRemainingLoginAttempts = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setupVariables();
}
public void Login(View view) {
if (username.getText().toString().equals("admin") && password.getText().toString().equals("secret")) {
Intent i = new Intent(login.this, MainActivity.class);
startActivity(i);
Toast.makeText(getApplicationContext(), "Welcome User",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Wrong Credentials",
Toast.LENGTH_SHORT).show();
numberOfRemainingLoginAttempts--;
attemptsLeft.setVisibility(View.VISIBLE);
numberOfRemainingLoginAttemptsTV.setVisibility(View.VISIBLE);
numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAttempts));
if (numberOfRemainingLoginAttempts == 0) {
login.setEnabled(false);
loginLocked.setVisibility(View.VISIBLE);
loginLocked.setBackgroundColor(Color.RED);
loginLocked.setText("Please Try Again Later");
}
}
}
private void setupVariables() {
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.button);
loginLocked = (TextView) findViewById(R.id.loginLocked);
attemptsLeft = (TextView) findViewById(R.id.attemptsLeft);
numberOfRemainingLoginAttemptsTV = (TextView) findViewById(R.id.numberOfRemainingLoginAttemptsTV);
numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAttempts));
}
}
“不工作”不说什么不顺心事情与您的代码。请改善描述,指出你的期望和发生的事情。 – Egor