我试图检查 if 中的字符串值,但它总是输入 else, 这里出了什么问题?谢谢
public void alertBtn(View v){
EditText text = (EditText)findViewById(R.id.editText1);
String value = text.getText().toString();
String password="asd";
if (value==password){
new AlertDialog.Builder(this)
.setTitle("Success")
.setMessage("Correct Password")
.setNeutralButton("OK", null)
.show();
}
else
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("Wrong password")
.setNeutralButton("OK", null)
.show();
}
最佳答案
使用==运算符将比较对字符串的引用,而不是字符串本身。
String value = text.getText().toString();
String password="asd";
if (value.equals(password))
{
}
关于java - 无法检查字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343369/