0

我在输入用户名和密码(如果两者相等)时创建了一个登录页面,它用于登录,但用户名和密码留空,当我点击模拟器上的登录按钮时,它显示登录成功它不应该登录时用户名和密码留空

 @Override
         public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

     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) {            

                    if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
                    Intent myIntent = new Intent(v.getContext(), SaveData.class);
                    startActivityForResult(myIntent, 1);
                    Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
                    } else{
                    Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show(); 
                    }
               }
           });
 }           
       }
4

5 回答 5

2

这是因为空白的用户名和密码也是相等的。您需要检查用户是否没有输入任何值,然后提示输入。更恰当地说,TextUtils.isEmpty用来做这项工作。

public void onClick(View v) { 
   String username = txtUserName.getText().toString();
   String password = txtPassword.getText().toString();

   if(!TextUtils.isEmpty(username) && username.equals(password)){

          //do your stuff
   }
}
于 2012-05-16T10:30:38.500 回答
2

首先尝试一些验证,无论任何一个字段是否为空白,您验证的代码都是

 btnLogin.setOnClickListener(new OnClickListener() {

                 @Override
                 public void onClick(View v) {            

                        if(txtUserName.getText().toString()).equals("")){
                             //Show Message
                        }else if(txtPassword.getText().toString().equals("")){
                             //Show Message
                        }else if(!(txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
                           Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show(); 
                        }else{
                            Intent myIntent = new Intent(v.getContext(), SaveData.class);
                            startActivityForResult(myIntent, 1);
                            Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
                        }
                   }
               });
于 2012-05-16T10:37:16.230 回答
1

您只需检查两者是否相等且均为空白,因此如果条件为真...

所以你应该检查两个字段是否为空......

像...

String strName = txtUserName.getText().toString();
String strPass = txtPwd.getText().toString();


     if(strName!=null&&strName.trim().length!=0&&strPass!=null && strPass.trim().length!=0){

    Intent myIntent = new Intent(v.getContext(), SaveData.class);
    startActivityForResult(myIntent, 1);
    Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show();

  }else{
                Toast.makeText(LoginappActivity.this, "input your name and password",Toast.LENGTH_LONG).show();
           }
于 2012-05-16T10:30:45.683 回答
0

只需添加 txtUserName.getText().toString().length() > 0条件&& operatorinif

if((txtUserName.getText().toString().length() > 0) && (txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
       Intent myIntent = new Intent(v.getContext(), SaveData.class);
       startActivityForResult(myIntent, 1);
       Toast.makeText(LoginappActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
       } else{
          Toast.makeText(LoginappActivity.this, "Invalid Login",Toast.LENGTH_LONG).show(); 
          }
     }
于 2012-05-16T10:31:51.027 回答
0

发生这种情况是因为""等于""(空字符串)

你可能想添加这个

[...]
if(txtUserName.getText().toString().length() > 0 && (txtUserName.getText().toString()).equals(txtPassword.getText().toString()))
[...]
于 2012-05-16T10:32:17.230 回答