从edittext Android

时间:2015-08-13 06:58:30

标签: java android parse-platform android-edittext

我想要一种方法,使用我的用户名或我的电子邮件登录我的Parse帐户。 我现在的代码只能使用我的用户名。我想知道一种识别文本字段是否包含用户名或电子邮件的方法。

这是代码,

private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();

        if (isFormInputValid(username_email, password)) {
            if (android.util.Patterns.EMAIL_ADDRESS.matcher(username_email).matches()) { // HERE!
                final String email = mUsernameEmailEtxt.getText().toString();
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {

                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }
}

private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();

        if (isFormInputValid(username_email, password)) {
            if (android.util.Patterns.EMAIL_ADDRESS.matcher(username_email).matches()) { // HERE!
                final String email = mUsernameEmailEtxt.getText().toString();
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {

                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }
}

更新:我已经记录日志并进行了一些更改(来源:@kevin),检测到电子邮件但拒绝使用它登录。

这是代码,

 private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();
        final String email = mUsernameEmailEtxt.getText().toString().toLowerCase();

        if (isFormInputValid(username_email, password)) {
            if (username_email.indexOf('@') != -1) { // HERE!
                Log.d("detector", "username_email detected as email:" + email.toString());
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                Log.d("detector", "username_email detected as username:" + username_email.toString());
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                            Log.d("error", "username or email invalid");
                        }

                    }
                });
            }

        }
    }
}

4 个答案:

答案 0 :(得分:2)

这并不难做,您应该使用Android中已提供的内置模式来完美检查电子邮件ID。

public final static boolean isEmailIDValid(CharSequence email) {
    if (email == null) 
        return false;

    return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

在API 8之上的所有Android版本上都不那么复杂和完美。我在任何支持API 14及更高版本的应用程序中都使用过此版本,并且从未遇到过任何问题。

答案 1 :(得分:1)

验证电子邮件地址extremely complicated。那篇文章建议您只是尝试向该地址发送一封电子邮件,这不完全符合您的使用案例,但请按照这些思路进行思考。也许做一个粗略的验证,比如测试该字段是否包含@,如果是,请尝试将其用作电子邮件。

答案 2 :(得分:0)

这不是那么复杂,你可以尝试this,如果电子邮件只包含低毛细管字母,@和“。”,它会给你真实的。

答案 3 :(得分:0)

使用Apache Validation Library。它小巧,简洁,做到了工作。

userEditText.addTextChangedListener(new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        textView.setVisibility(View.VISIBLE);
    }

    public void afterTextChanged(Editable s) {
        EmailValidator emailValidator = EmailValidator.getInstance();
        if (!emailValidator.isValid(s.toString())) {
          //Is Email
        } else {
         //Is username   
        }
    }
});

注意:希望您已考虑到边缘情况,因为您的用户名也可能是根据您的要求判断的电子邮件