没有输入用户名和密码我的应用程序正在记录

时间:2012-05-16 10:27:51

标签: android

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

 @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(); 
                    }
               }
           });
 }           
       }

5 个答案:

答案 0 :(得分: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
   }
}

答案 1 :(得分: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();
                        }
                   }
               });

答案 2 :(得分: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();
           }

答案 3 :(得分:0)

只需在txtUserName.getText().toString().length() > 0

中添加条件&& operatorif
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(); 
          }
     }

答案 4 :(得分:0)

这是因为""等于""(空字符串)

您可能想要添加此

[...]
if(txtUserName.getText().toString().length() > 0 && (txtUserName.getText().toString()).equals(txtPassword.getText().toString()))
[...]