Java Can';t检查字符串值
我试图在if中检查字符串值,但它总是输入else, 这里怎么了?多谢各位Java Can';t检查字符串值,java,android,if-statement,Java,Android,If Statement,我试图在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 AlertDi
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();
}
使用
.equals
或.equalsIgnoreCase()
比较字符串
同时将editText的初始化移动到onCreate
。无需每次单击按钮时初始化edittext
text = (EditText)findViewById(R.id.editText1); // in onCreate
并声明
EditText
为类成员使用.equals
或.equalsIgnoreCase()
来比较字符串
同时将editText的初始化移动到onCreate
。无需每次单击按钮时初始化edittext
text = (EditText)findViewById(R.id.editText1); // in onCreate
并将EditText
声明为类成员使用equals()函数
试一试
使用equals()函数
试一试
使用=运算符将引用与字符串进行比较,而不是与字符串本身进行比较
String value = text.getText().toString();
String password="asd";
if (value.equals(password))
{
}
使用=运算符将引用与字符串进行比较,而不是与字符串本身进行比较
String value = text.getText().toString();
String password="asd";
if (value.equals(password))
{
}
看
使用String.equals(String other)
函数比较字符串,而不是使用=
运算符
函数检查字符串的实际内容,=
操作符检查对对象的引用是否相等。请注意,字符串常量通常是“插入”的,因此具有相同值的两个常量实际上可以与=
进行比较,但最好不要依赖于此
使用String.equals(String other)
函数比较字符串,而不是使用=
运算符
函数检查字符串的实际内容,
=
操作符检查对对象的引用是否相等。请注意,字符串常量通常是“interned”的,因此具有相同值的两个常量实际上可以与=
进行比较,但最好不要依赖于此。请。。。不要使用==
来比较字符串。请不要使用equals()函数对于回答的人,您可能还需要给出解释。。。。。特别是当这个问题已经被回答了1000次,你想让我解释什么?我只是想做一个登录屏幕。请。。。不要使用==
来比较字符串。请不要使用equals()函数对于回答的人,您可能还需要给出解释。。。。。特别是当这个问题已经被回答了1000次,你想让我解释什么?我只是想做一个登录屏幕。非常感谢,有没有我可以找到基本命令的网站?非常感谢,有没有我可以找到基本命令的网站?