我正在制作一个需要设置页面密码的程序。目前我的做法是弹出一个带有文本字段和密码请求的警报。当输入正确的密码(目前硬编码为“测试”但会更改)时,它应该启动设置 Activity ,密码不正确会导致警告说“不正确”。
目前,无论密码正确还是错误,它都只是关闭警报,没有消息或任何东西。
我边学边学,所以我确定我已经严重破坏了代码,有什么建议吗?
public void Settings(View view) {
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Main User Only");
alert.setMessage("Please enter password");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
if (value == "test")
{Intent settings = new Intent(Bootscreen.this, ItemListActivity.class);
startActivity(settings);}
else
if (!(value == "test")) { alert.setMessage("Incorrect");}}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
最佳答案
使用
if (value.equals("test"))
代替
if (value == "test")
用于比较字符串
关于android - 当用户按确定时,密码提示不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13523134/