c# - 为什么我的 if 语句不起作用?我的代码有什么问题?
问题描述
因此,当我在 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。
解决方案
username
并且password
是空白字符串,因为应用程序启动时文本框是空白的。你需要搬家
string username = userInput.Text;
string password = passInput.Text;
inside ofCheckCredentials
检索用户实际输入的内容。
推荐阅读
- html - 类不应用角度跨度颜色?
- security - 如何避免在 chrome 网络控制台上显示会话和 firebase 详细信息
- vb.net - 在Windows 10上滚动图片框时VB.Net图片框绘制太慢
- javascript - 我无法在我的 React 应用程序中修改我的 SVG 组件
- excel - Excel上的交互式下拉框
- c# - “XImage”不包含“GetFrameCount”的定义
- regex - 我认为这段代码可以找到字符串长度。这里到底发生了什么?
- vue.js - Vue - 如何为对象数组中的对象添加反应属性?
- c# - 如何实时更新文本块?
- php - 注意 - 数组到字符串转换错误