【发布时间】:2013-02-27 01:22:06
【问题描述】:
我的活动是一个简单的登录活动,其中(仅用于测试)如果用户名和密码相等则返回 true,否则返回 false。
但它总是返回 false。
即使我转换toString(); 示例:
String a=(txtUserName.getText().toString() == txtPassword.getText().toString()) ? "equal" : "Nequal";
完整代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtUserName = (EditText) this.findViewById(R.id.txtUname);
txtPassword = (EditText) this.findViewById(R.id.txtPwd);
btnLogin = (Button) this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(LoginActivity.this,txtUserName.getText(),Toast.LENGTH_LONG).show();
Toast.makeText(LoginActivity.this,"Just for separate", Toast.LENGTH_LONG).show();
Toast.makeText(LoginActivity.this,txtPassword.getText(), Toast.LENGTH_LONG).show();
String a=(txtUserName.getText() == txtPassword.getText()) ? "equal" : "Nequal";
Toast.makeText(LoginActivity.this, a, Toast.LENGTH_LONG).show();
}
});
}
【问题讨论】:
-
你把比较指针的 == 和比较内容的 .equals() 混淆了。
标签: java android string operators