为什么我的if语句不起作用?我的代码有什么问题?

时间:2018-12-26 21:41:29

标签: c# xamarin

因此,当我在EditText栏中输入适当的凭据时,仍然会收到ACCESS DENIED消息。应该发生的是,我应该获得ACCESS GRANTED消息,而不是ACCESS DENIED。那么我的if语句怎么了?

        LinearLayout layout = new LinearLayout(this);
        layout.Orientation = Orientation.Vertical;

        TextView userText = new TextView(this);
        TextView passText = new TextView(this);

        EditText userInput = new EditText(this);
        EditText passInput = new EditText(this);

        TextView access = new TextView(this);
        access.Text = "";

        Button loginButton = new Button(this);
        loginButton.Text = "Login";

        userText.Text = "Username";
        passText.Text = "Password";

        userInput.SetMaxLines(1);
        passInput.SetMaxLines(1);

        string username = userInput.Text;
        string password = passInput.Text;

        loginButton.Click += (sender, e) =>
        { CheckCredentials(); };

        void CheckCredentials()
        {
            if (username == "Admin" && password == "adminpass")
            {
                access.Text = "ACCESS GRANTED!";
            }
            else
            {
                access.Text = "ACCESS DENIED!";
            }
        }

        SetContentView(layout);
        layout.AddView(userText);
        layout.AddView(userInput);
        layout.AddView(passText);
        layout.AddView(passInput);
        layout.AddView(loginButton);
        layout.AddView(access);

当您将“ Admin”作为用户名并将“ adminpass”作为密码时,应打印出ACCESS GRANTED。

1 个答案:

答案 0 :(得分:1)

usernamepassword是空白字符串,因为在应用程序启动时文本框为空白。您需要移动

string username = userInput.Text;
string password = passInput.Text;
CheckCredentials

检索用户实际键入的内容。