我希望有人可以提供帮助!我对android很新,我遇到了textfields的问题。我有3个文本字段要求用户输入信息,当单击注册按钮时,消息将让用户知道他们已注册并将其重定向到登录活动。 我的问题是我想检查用户是否已完成所有3个文本字段,如果没有显示错误消息并允许他们再试一次。我可以显示错误消息,但即使所有文本字段都没有完成,如果单击reg按钮它仍然告诉用户他们已注册并将用户重定向到登录活动,如果有人可以告诉我我错了,我已经发布了我的代码,提前谢谢!
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String data1 = inputName.getText().toString();
if(data1.trim().equals("")){
Toast.makeText(RegisterActivity.this, "Please enter your name",
Toast.LENGTH_SHORT).show();
}
String data2 = inputEmail.getText().toString();
if(data2.trim().equals("")){
Toast.makeText(RegisterActivity.this, "please enter email address",
Toast.LENGTH_SHORT).show();
}
String data3 = inputPassword.getText().toString();
if(data3.trim().equals("")){
Toast.makeText(RegisterActivity.this, "Please enter a password",
Toast.LENGTH_SHORT).show();
}
if(data1 != null && data2 != null && data3!= null){
mySQLiteAdapter.insert(data1, data2, data3);
updateList();
Toast.makeText(RegisterActivity.this, "You are now registered",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
inputName.setText("");
inputEmail.setText("");
inputPassword.setText("");
}
else Toast.makeText(RegisterActivity.this, "Error please try again!",
Toast.LENGTH_SHORT).show();
}
};
答案 0 :(得分:3)
下面:
if(data1 != null && data2 != null && data3!= null)
这些不是null
,只是空字符串(""
)。这总是如此。
答案 1 :(得分:0)
您也可以使用TextUtils.isEmpty(string)
if (!TextUtils.isEmpty(data1) && !TextUtils.isEmpty(data2) && !TextUtils.isEmpty(data2)) {
Log.d(TAG, "All three Strings are not empty or null!");
}
答案 2 :(得分:0)
感谢您的有用答案,我想我已经解决了我的问题!希望我的解决方案可以帮助别人!
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String data1 = inputName.getText().toString();
String data2 = inputEmail.getText().toString();
String data3 = inputPassword.getText().toString();
boolean invalid = false;
if(data1.equals("")){
invalid = true;
Toast.makeText(getApplicationContext(), "Username Missing", Toast.LENGTH_SHORT).show();
}else if(data2.equals("")){
invalid = true;
Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
}else if(data3.equals("")){
invalid = true;
Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
}
if(invalid == false){
mySQLiteAdapter.insert(data1, data2, data3);
updateList();
Toast.makeText(RegisterActivity.this, "You are now registered",
Toast.LENGTH_SHORT).show();
Intent i_register = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(i_register);
inputName.setText("");
inputEmail.setText("");
inputPassword.setText("");
finish();
}
/*mySQLiteAdapter.insert(data1, data2, data3);
updateList();
Toast.makeText(RegisterActivity.this, "You are now registered",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));*/
}
};