从 EditText 获取字符串
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if( username_text.getText().toString() == "admin" &&
pass_text.getText().toString() == "password"){
startActivityForResult(i, ACTIVITY_CREATE);
}else{
Toast.makeText(gps_gui.this, "Your username or password is not correct! Please, insert again",
Toast.LENGTH_SHORT).show();
clearEditText();
}
}
});
我尝试检查它,发现它不符合 if 条件。
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if( username_text.getText().toString() == "admin" &&
pass_text.getText().toString() == "password"){
startActivityForResult(i, ACTIVITY_CREATE);
}else{
Toast.makeText(gps_gui.this, "Your username or password is not correct! Please, insert again",
Toast.LENGTH_SHORT).show();
clearEditText();
}
}
});
I'm try to check it, I found it not go to if condition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用java时,必须使用String.equals(String)方法来比较字符串。 == 比较检查 String 对象值是否相等,毫无疑问它们不相等。尝试将示例更改为:
将静态字符串放在前面也是明智的,以防正在检查的值的字符串值为空。
When using java, you must compare Strings using the String.equals(String) method. The == comparison checks to see if the String object values are equal, which undoubtedly they are not. Try to change you example to :
Its also smart to put the static string first, in case the string value of the value being checked is null.
使用 String 类的 equals() 方法来比较字符串。以下链接将为您提供更多详细信息。
关于equals(Object yourObj) 方法(区分大小写)
equalsIgnoreCase(String yourStringObject) 可以使用,如果你希望它不区分大小写!
Use String class's equals() method to compare Strings. The following links would give you more details.
About equals(Object yourObj) method (CASE sensitive)
equalsIgnoreCase(String yourStringObject) can be used, if you want it to be not case sensitive!