我正在尝试创建一个简单的登录页面,使用正确的用户名和密码将转到另一个活动(已经创建)此外,尝试计数器无法正常运行。对不起,如果说这个措辞不好,希望它有意义。
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));
}
}
答案 0 :(得分:-1)
当你使用startActivity()
时,你必须在上下文中使用它(并不总是。仅在某些条件下,但我认为在上下文中声明它是一个好习惯)。因此,将startActivity(i)
更改为login.this.startActivity(i);
希望这应该让它运行。 希望能帮助到你!
编辑:也请发布您的布局文件。并在您的公共虚空登录中添加此
请记住:直接将其放在空白中而不是if
语句
Log.i("LOGIN DETAILS","Username: "+username.getText().toString()+"Password: "+password.getText().toString());
然后使用信息过滤器发布您的日志猫。
如果对日志猫不太好,您可以在公共无效登录中添加此行代码而不是if
语句
Toast.makeText(login.this, "Username: "+ username.getText().toString() + "password: " + password.getText().toString(), Toast.LENGTH_LONG).show();