[英]Android EditText Value Error?
I'm testing out some stuff on android as a beginner and was trying to grab value entered in a EditText when a button was clicked , then compare it to a string value that I defined inside the class, then use if (EditText == stringDefined) else () , but my code always jumps on the else part even if the correct text is entered, any help is appreciated. 我正在初学者上测试android上的一些东西,并试图抓住单击按钮时在EditText中输入的值,然后将其与我在类内定义的字符串值进行比较,然后使用if(EditText == stringDefined )else(),但是即使输入正确的文本,我的代码也会始终跳转到else部分,请多多帮助。 Here is the code :
这是代码:
Button mButton;
EditText mEdit1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final String user = "admin";
mButton = (Button)findViewById(R.id.BTN_login);
mEdit1 = (EditText)findViewById(R.id.editText1);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String userEntered = mEdit1.getText().toString().trim();
Intent intent = new Intent(Login.this, Success.class);
if(userEntered == user){
startActivity(intent);
}
else{
AlertDialog.Builder errAlert = new AlertDialog.Builder(Login.this);
errAlert.setTitle("Wrong Credentials");
errAlert.setMessage("Wrong username");
errAlert.setCancelable(true);
errAlert.show();
}
}
});
}
Use equals method : 使用equals方法:
if(userEntered.equals(user)){
startActivity(intent);
}
==
用于比较原始类型,而java.lang.String
类的equals()
方法比较内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.