gpt4 book ai didi

java - 简单的 Android 密码检查器不起作用

转载 作者:行者123 更新时间:2023-12-01 18:18:47 24 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,将打开的第一个 Activity 是密码输入。要知道密码已经设置在值为“admit”的变量中。我创建了一个 if 语句来检查用户输入的值,然后将其与变量进行比较,如果值匹配,则转到主屏幕,但它一直说这是错误的密码。任何人都可以看一下我的代码并建议我是否出错了。

public class Password extends ActionBarActivity implements View.OnClickListener {
protected String password = "admin";
String getPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
Button passwordButton = (Button) findViewById(passwordbutton);
EditText passwordInput = (EditText) findViewById(R.id.password);
getPassword = (passwordInput.getText().toString());
passwordButton.setOnClickListener(this);

}

public void onClick(View v) {

if (getPassword.equals(password)) {
Intent goHome;
goHome = new Intent(this, home.class);
startActivity(goHome);
} else {
AlertDialog.Builder wrongPasswordBuilder = new AlertDialog.Builder(this);
wrongPasswordBuilder.setTitle(getString(R.string.wrongPasswordTitle));
wrongPasswordBuilder.setMessage(getString(R.string.wrongPasswordTryAgain));
wrongPasswordBuilder.setPositiveButton("ok", null);
AlertDialog dialog = wrongPasswordBuilder.show();
}
}

最佳答案

如果你在onCreate()中编写passwordInput.getText().toString(),它总是返回空字符串。因此,请在 onClick() 中写入这些行。代码写在下面

public void onClick(View v) {
getPassword = passwordInput.getText().toString()
if (getPassword.equals(password)) {
Intent goHome;
goHome = new Intent(this, home.class);
startActivity(goHome);
} else {
AlertDialog.Builder wrongPasswordBuilder = new AlertDialog.Builder(this);
wrongPasswordBuilder.setTitle(getString(R.string.wrongPasswordTitle));
wrongPasswordBuilder.setMessage(getString(R.string.wrongPasswordTryAgain));
wrongPasswordBuilder.setPositiveButton("ok", null);
AlertDialog dialog = wrongPasswordBuilder.show();
}
}

关于java - 简单的 Android 密码检查器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28153246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com