为什么我的登录和注册不起作用甚至无法重定向?

时间:2019-03-26 19:01:26

标签: java android

我正在制作一个“登录”应用程序,但实际的“登录”按钮不起作用。该页面上的所有其他内容都可以正常工作,例如我实现的计数器可以对登录尝试次数进行计数。计数器一旦达到0,便具有禁用按钮功能。我还尝试过在页面上实施注册,该操作应将我重定向到注册表单,否则我的应用程序将崩溃,即使使用正确的方法也不会登录我证书。

下面是主要活动中的代码:

//creating variables
private EditText Username;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 5;
private TextView userRegistration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //assigning the variables to the XML id's
    Username = (EditText)findViewById(R.id.USERNAMEET);
    Password = (EditText)findViewById(R.id.PASSWORDET);
    Info = (TextView)findViewById(R.id.INFOTV);
    Login = (Button)findViewById(R.id.LOGINBTN);
    userRegistration = (TextView)findViewById(R.id.REGISTERTV);

    Info.setText("Number of attempts remaining: 5");
    Login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //takes in the userinput
            validate(Username.getText().toString(),Password.getText().toString());
        }
    });

    userRegistration.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this, RegistrationActivity.class));
        }
    });

}


//function to validate the username and password
private void validate(String userName, String userPassword){
    if((userName == "admin") && (userPassword == "changeme")){
        //following lets the user into a new activity e.g. redirect
        Intent intent = new Intent(MainActivity.this, Averages.class);
        startActivity(intent);
    }else{
        counter--;

        Info.setText("Number of attempts remaning: " + String.valueOf(counter));
        Log.d("MyApp","I am here: " + Username.toString() + ", ok.");


        if(counter == 0){
            Login.setEnabled(false);
        }
    }
}

0 个答案:

没有答案