将可编辑内容转换为字符串时出现问题(用户名/密码测试)
我正在尝试创建一个简单的用户名/密码登录屏幕。我已经完成布局,现在,我正在尝试设置它,以便当用户名 (EditText) == "crete"
时,它应该执行某些操作。这是我的代码...:
public class Login extends Activity {
public static EditText username, password;
public Button loginbutton;
boolean accessgranted;
public String dbu, dbp, user1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText) this.findViewById(R.id.username);
password = (EditText) this.findViewById(R.id.password);
loginbutton = (Button) this.findViewById(R.id.loginbutton);
user1 = "crete";
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
dbu = (username.getText()).toString();
}
finally{
if (dbu == user1){
username.setText("SUCCESS");
}
}
}
});
}
}
遗憾的是,这不起作用。它正确地将其转换为字符串(我认为),因为当我测试此代码时:
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
dbu = (username.getText()).toString();
}
finally{
username.setText("done" + dbu);
}
}
}
});
它正确输入您在 EditText
中输入的内容,加上单词“done”。
创建 if-then 语句似乎有问题?
I'm trying to create a simple username/password login screen. I have the layout done, and right now, I'm trying to set it so when the username (EditText) == "crete"
, then it should do something. Here is my code...:
public class Login extends Activity {
public static EditText username, password;
public Button loginbutton;
boolean accessgranted;
public String dbu, dbp, user1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText) this.findViewById(R.id.username);
password = (EditText) this.findViewById(R.id.password);
loginbutton = (Button) this.findViewById(R.id.loginbutton);
user1 = "crete";
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
dbu = (username.getText()).toString();
}
finally{
if (dbu == user1){
username.setText("SUCCESS");
}
}
}
});
}
}
this, sadly, doesn't work. It correctly converts it to a string (i think) because when I tested this code out :
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
dbu = (username.getText()).toString();
}
finally{
username.setText("done" + dbu);
}
}
}
});
It correctly enters what you entered into the EditText
, plus the word "done".
There seems to be a problem with creating if-then statements??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
.equals("String")
方法测试String
相等性。使用
==
您可以测试对象的引用是否相等。You test for
String
equality with the method.equals("String")
.With
==
you are testing if the references to the objects are equal.尝试使用
equalsIgnoreCase(String)
而不是==
比较器。像这样:
dbu.equalsIgnoreCase(user1)
Try using
equalsIgnoreCase(String)
instead of the==
comparator.Like this:
dbu.equalsIgnoreCase(user1)
dub
和user1
是两个独立的 String 对象。您可以这样比较它们:dbu == user1
。这将始终返回 false。相反,请将其替换为dbu.equals(user1)
。dub
anduser1
are two separate String objects. You're comparing them like this:dbu == user1
. This will always return false. Instead, replace it withdbu.equals(user1)
.