不检查验证移动到android中的下一个活动

时间:2014-09-26 10:07:12

标签: android validation

您好我的应用程序我有用户名和密码以及登录按钮。点击登录提交按钮,不检查验证,空字段也转到下一个活动。

我想检查是否需要用户名和密码,点击登录按钮转到下一个活动。

Login.java

public class Login extends Activity {
    Button login;
    private static final Pattern USERNAME_PATTERN = Pattern
            .compile("[a-zA-Z0-9]{1,250}");
    private static final Pattern PASSWORD_PATTERN = Pattern
            .compile("[a-zA-Z0-9+_.]{4,16}");
    EditText usname,pword;
    TextView tv;
    String result=null;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    CheckBox mCbShowPwd;
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;

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

        login = (Button)findViewById(R.id.login);  
        usname = (EditText)findViewById(R.id.username);
        pword= (EditText)findViewById(R.id.password);
        mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);
       mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // checkbox status is changed from uncheck to checked.
                if (!isChecked) {
                        // show password
                    usname.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                        // hide password
                     pword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });

       login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String username = usname.getText().toString();
                final String password = pword.getText().toString();
                if (username.equals("") || password.equals("")) {
                    if (username.equals("")) {
                        Toast.makeText(Login.this, "ENTER USERNAME",
                                Toast.LENGTH_LONG).show();

                    }
                    if (password.equals("")) {
                        Toast.makeText(Login.this, "ENTER PASSWORD",
                                Toast.LENGTH_LONG).show();

                    }

                } else {
                    if (!CheckUsername(username)) {
                        Toast.makeText(Login.this, "ENTER VALID USERNAME",
                                Toast.LENGTH_LONG).show();
                    }
                    if (!CheckPassword(password)) {
                        Toast.makeText(Login.this, "ENTER VALID PASSWORD",
                                Toast.LENGTH_LONG).show();
                    }

                }

                final String queryString = "username=" + username + "&password="
                        + password;
                String result = DatabaseUtility.executeQueryPhp("login",queryString);

                 Intent i = new Intent(getApplicationContext(), Home.class);
                    startActivity(i);







                        }
                      });



            }

            private boolean CheckPassword(String password) {

                return PASSWORD_PATTERN.matcher(password).matches();
            }

            private boolean CheckUsername(String username) {

                return USERNAME_PATTERN.matcher(username).matches();
            }

3 个答案:

答案 0 :(得分:0)

使用此选项检查用户名并以相同方式检查密码

public static boolean isValidUsername(EditText ed) {
    boolean isValidUsername = true;
    if (ed != null) {
        Pattern r = Pattern.compile(USERNAME_PATTERN);
        String s = ed.getText().toString().trim();
        String s1 = s.toString();
        Matcher m = r.matcher(s1);
        if (m.find()) {
            ed.setError("You are allowed to use a-z, A-Z, 0-9, ***") // *** are your                                 other possible symbols
            isValidUsername = false;
        } else if (s.length() < 5) {
            ed.setError("Atleast five characters must be there in username");
            isValidUsername = false;
        }
    }
    return isValidUsername;
}

现在

if(editetxt_username.isValidUsernam() && editetxt_password.isValidPassword()) {
   //Change to new activity here
}

答案 1 :(得分:0)

您可以使用:

TextUtils.isEmpty(CharSequence);

示例:

Editable username  = txtUsername.getText();
if(TextUtils.isEmpty(username)) {
    // username field is empty
}
else {
   // username field is not empty , TODO check the pattern of the username 
}

PS:对密码字段做同样的事情。

答案 2 :(得分:0)

login.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final String username = usname.getText().toString();
            final String password = pword.getText().toString();
            if (username.equals("") || password.equals("")) {
                if (username.equals("")) {
                    Toast.makeText(Login.this, "ENTER USERNAME",
                            Toast.LENGTH_LONG).show();

                }
                if (password.equals("")) {
                    Toast.makeText(Login.this, "ENTER PASSWORD",
                            Toast.LENGTH_LONG).show();

                }

            } else if (!CheckUsername(username) && !CheckPassword(password)){
                    Toast.makeText(Login.this, "ENTER VALID USERNAME & PASSWORD",
                          Toast.LENGTH_LONG).show();
            }else{
             Intent i = new Intent(getApplicationContext(), Home.class);
                startActivity(i);
            }
         }
      });