我有2个stringBuilder值电子邮件和密码在某些地方被清除(删除)但我想保留它,如果一个是正确的。我认为我的逻辑是正确的,但值继续告诉我在if语句之后它是空的... else if(email.toString()。equals(login_test_name)&& password.toString()。isEmpty() || password.toString()。equals(login_test_name)&& email.toString()。的isEmpty())
public class MainActivity extends ActionBarActivity {
//variables
String login_test_name = "t";
String login_test_pass = "t";
EditText username;
EditText pass;
private StringBuilder email = new StringBuilder();
private StringBuilder password = new StringBuilder();
//Remember password variables
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Remove action bar from screen
setContentView(R.layout.activity_main);
//get EditText field data by id name
username = (EditText)findViewById(R.id.emailTxt);
pass = (EditText)findViewById(R.id.passTxt);
}//end oncreate
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//code to change activity with BTN click (to register user)
public void registerUser(View v)
{
Intent i = new Intent(MainActivity.this, UserRegister.class );
startActivity(i);
}
// code to change activity (to order menu if username and pass are ture)
public void orderMenu(View v)
{
// change EditText type to String type
//String u = username.getText().toString();
//String p = pass.getText().toString();
email.append(username.getText().toString());
password.append(pass.getText().toString());
//test login username and password
if (!email.toString().equals(login_test_name) || !password.toString().equals(login_test_pass) ||
email.toString().isEmpty() || password.toString().isEmpty())
{
if(!email.toString().equals(login_test_name) && !password.toString().equals(login_test_pass) ||
email.toString().isEmpty() && password.toString().isEmpty())
{
//if fields are not empty but are not the same
if(!email.toString().equals(login_test_name) && !password.toString().equals(login_test_pass) &&
!email.toString().isEmpty() && !password.toString().isEmpty())
{
Toast.makeText(this, "Invalid Email and Password", Toast.LENGTH_LONG).show();
//clear text fields
username.setText("");
pass.setText("");
email.delete(0, email.length());
password.delete(0, password.length());
}
//else the fields are empty
else
{
Toast.makeText(this, "Email and Password fields are empty", Toast.LENGTH_LONG).show();
}
}
//if one field is NOT correct but the other is and the field is NOT empty
else if(!email.toString().equals(login_test_name) && password.toString().equals(login_test_name) && !email.toString().isEmpty()
|| !password.toString().equals(login_test_pass) && email.toString().equals(login_test_name) && !password.toString().isEmpty())
{
if(!email.toString().equals(login_test_name) && password.toString().equals(login_test_name) &&
!email.toString().isEmpty())
{
Toast.makeText(this, "Invalid Email but \nPassword is correct", Toast.LENGTH_LONG).show();
email.delete(0, email.length());
username.setText("");
}
else
{
Toast.makeText(this, "Invalid Password but \nEmail is correct", Toast.LENGTH_LONG).show();
password.delete(0, password.length());
pass.setText("");
}
}
//if one fields is correct but the other is EMPTY
else if(email.toString().equals(login_test_name) && password.toString().isEmpty()
|| password.toString().equals(login_test_name) && email.toString().isEmpty())
{
if(email.toString().equals(login_test_name) && password.toString().isEmpty())
{
Toast.makeText(this, "Email is correct but \nPassword field is empty",
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Password is correct but \nEmail field is empty",
Toast.LENGTH_LONG).show();
}
}
// Add if statement for if text is missing in fields
/*StringBuilder errorMessage = new StringBuilder();
errorMessage.append("wrong username or password\n");
errorMessage.append("username: " + u + "\n");
errorMessage.append("pass: " + p + "\n");
errorMessage.append(login_test_name + "\n");
errorMessage.append(login_test_pass + "\n");
Toast.makeText(this, errorMessage.toString(), Toast.LENGTH_LONG).show();*/
}//end if for invalid and empty text fields
//else credentials gain access
else if (email.toString().equals(login_test_name) && password.toString().equals(login_test_pass))
{
// change to /order menu activity
Intent i = new Intent(MainActivity.this, OrderMenu.class );
startActivity(i);
// message to user
Toast.makeText(this, "logged in", Toast.LENGTH_SHORT).show();
email.delete(0, email.length());
password.delete(0, password.length());
username.setText("");
pass.setText("");
}//end else
}//end order menu method
//cancel app button
public void cancelApp(View v)
{
finish();
username.setText("");
pass.setText("");
System.exit(0);
}//end cancel app method
//change activity to forgot password
public void forgotPassword(View v)
{
Intent i = new Intent(MainActivity.this, ForgotPassword.class );
startActivity(i);
}//end forgotPassword method
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}//end main activity class
答案 0 :(得分:0)
你可以让它变得更简单。
if (!email.toString().equals(login_test_name)) {
Toast.makeText(this, "Invalid Email", Toast.LENGTH_LONG).show();
email.setLength(0);
}
if (!password.toString().equals(login_test_pass)) {
Toast.makeText(this, "Invalid Password", Toast.LENGTH_LONG).show();
password.setLength(0);
}
if (email.length() > 0 && password.length > 0) {
// start your activity
}