本文介绍了上的EditText电子邮件验证在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的code进行登录,但是当我的电子邮件-ID后键入\\,则接受并成功登录(它不接受任何其他符号或字符只接受\\)。

我不希望它与\\登录。`

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    尝试{
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow()。setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        的setContentView(R.layout.main);
        WiztangoBaseApplication.InitDialogBox(WiztangoActivity.this);        这preF = preferenceManager.getDefaultShared preferences(本)。
        loginStatus =(的TextView)findViewById(R.id.login_status);
        注册=(按钮)findViewById(R.id.btnregister);
        register.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                RegisterIntent();
            }
        });        登录=(按钮)findViewById(R.id.btlogin);
        密码=(EditText上)findViewById(R.id.txtPwd);
        用户名=(的EditText)findViewById(R.id.txtemail);
        saveLoginCheckBox =(复选框)findViewById(R.id.saveLoginCheckBox);
        preF = getShared preferences(常量。preFS_NAME,
                MODE_PRIVATE);
        字符串usernamestr = pref.getString(常量preF_USERNAME,);
        字符串passwordsharestr = pref.getString(常量。preF_PASSWORD,
        );
        username.setText(usernamestr);
        password.setText(passwordsharestr);
        saveLogin = pref.getBoolean(saveLogin,FALSE);
        如果(saveLogin ==真){
            username.setText(usernamestr);
            password.setText(passwordsharestr);
            saveLoginCheckBox.setChecked(真);
        }        login.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                尝试{
                    //Constants.clearInfo();
                    getShared preferences(常量。preFS_NAME,MODE_PRIVATE)
                    。编辑()
                    .putString(常量。preF_USERNAME,
                            username.getText()。的toString())
                            .putString(常量。preF_PASSWORD,
                                    password.getText()。的toString())
                                    .putString(常量。preF_CHECKED,TRUE)
                                    。承诺();
                    如果(username.getText()。的toString()。修剪()。等于()){
                        username.setError(HTML
                                .fromHtml(请输入用户名));
                        username.requestFocus();
                    }否则如果(password.getText()。的toString()。修剪()
                            。等于()) {
                        password.setError(HTML
                                .fromHtml(请输入密码));
                        password.requestFocus();
                    }其他{
                        如果(常量
                                .checkInternetConnection(WiztangoActivity.this)){
                            Constants.userpass = password.getText()
                            的ToString();
                            新AuthenticData()执行();
                        }其他{
                            WiztangoBaseApplication.ShowThisDialog(错误,
                            请检查网络连接。);
                        }                        如果(saveLoginCheckBox.isChecked()){                            prefEditor.putBoolean(saveLogin,真正的);
                            prefEditor.putString(常量。preF_USERNAME,
                                    。username.getText()的toString());
                            prefEditor.putString(常量。preF_PASSWORD,
                                    。password.getText()的toString());
                            saveLoginCheckBox.setChecked(真);
                            prefEditor.commit();
                        }其他{                            共享preferences米preferences = getShared preferences(常量preFS_NAME,MODE_PRIVATE);
                            共享preferences.Editor编辑= M preferences.edit();                            editor.remove(常量preF_USERNAME);
                            editor.remove(常量preF_PASSWORD);
                            editor.commit();
                        }
                    }                }赶上(例外五){
                    Log.e(例外Wiztango /,e.toString());
                    e.printStackTrace();
                }
            }
        });
    }赶上(例外五){
        Log.e(例外Wiztango /,e.toString());
        e.printStackTrace();
    }
}


解决方案

内置模式提供电子邮件验证,如:

 如果(!android.util.Patterns.EMAIL_ADDRESS.matcher(emailStr).matches()及和放大器;!TextUtils.isEmpty(emailStr)){
    emailEditText.setError(无效的电子邮件);
    emailEditText.requestFocus();
}

I wrote following code for login but when I type "\" after email-id, it accepts and log in successfully (it doesn't accepts any other symbols or characters only accepts "\").

I don't want that it login with "\".`

@Override
public void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.main);
        WiztangoBaseApplication.InitDialogBox(WiztangoActivity.this);

        this.pref = PreferenceManager.getDefaultSharedPreferences(this);
        loginStatus = (TextView)findViewById(R.id.login_status);
        register = (Button) findViewById(R.id.btnregister);
        register.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RegisterIntent();
            }
        });

        login = (Button) findViewById(R.id.btlogin);
        password = (EditText) findViewById(R.id.txtPwd);
        username = (EditText) findViewById(R.id.txtemail);
        saveLoginCheckBox = (CheckBox)findViewById(R.id.saveLoginCheckBox);
        pref = getSharedPreferences(Constants.PREFS_NAME,
                MODE_PRIVATE);
        String usernamestr = pref.getString(Constants.PREF_USERNAME, "");
        String passwordsharestr = pref.getString(Constants.PREF_PASSWORD,
        "");
        username.setText(usernamestr);
        password.setText(passwordsharestr);
        saveLogin = pref.getBoolean("saveLogin", false);
        if (saveLogin == true) {
            username.setText(usernamestr);
            password.setText(passwordsharestr);
            saveLoginCheckBox.setChecked(true);
        }

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    //Constants.clearInfo();
                    getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE)
                    .edit()
                    .putString(Constants.PREF_USERNAME,
                            username.getText().toString())
                            .putString(Constants.PREF_PASSWORD,
                                    password.getText().toString())
                                    .putString(Constants.PREF_CHECKED, "TRUE")
                                    .commit();


                    if (username.getText().toString().trim().equals("")) {
                        username.setError(Html
                                .fromHtml("Please Enter Username"));
                        username.requestFocus();
                    } else if (password.getText().toString().trim()
                            .equals("")) {
                        password.setError(Html
                                .fromHtml("Please Enter Password"));
                        password.requestFocus();
                    } else {
                        if (Constants
                                .checkInternetConnection(WiztangoActivity.this)) {
                            Constants.userpass = password.getText()
                            .toString();
                            new AuthenticData().execute();
                        } else {
                            WiztangoBaseApplication.ShowThisDialog("Error",
                            "Please check internet connection.");
                        }

                        if (saveLoginCheckBox.isChecked()) {

                            prefEditor.putBoolean("saveLogin", true);
                            prefEditor.putString(Constants.PREF_USERNAME,
                                    username.getText().toString());
                            prefEditor.putString(Constants.PREF_PASSWORD,
                                    password.getText().toString());
                            saveLoginCheckBox.setChecked(true);
                            prefEditor.commit();
                        } else {

                            SharedPreferences mPreferences = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
                            SharedPreferences.Editor editor=mPreferences.edit();

                            editor.remove(Constants.PREF_USERNAME);
                            editor.remove(Constants.PREF_PASSWORD);
                            editor.commit();


                        }
                    }

                } catch (Exception e) {
                    Log.e("Exception In Wiztango/", e.toString());
                    e.printStackTrace();
                }
            }
        });
    } catch (Exception e) {
        Log.e("Exception In Wiztango/", e.toString());
        e.printStackTrace();
    }
}

解决方案

Inbuilt Patterns Provides Email Validation like:

if (!android.util.Patterns.EMAIL_ADDRESS.matcher(emailStr).matches() && !TextUtils.isEmpty(emailStr)) {
    emailEditText.setError("Invalid Email");
    emailEditText.requestFocus();
}

这篇关于上的EditText电子邮件验证在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 19:39