对话框

时间:2016-04-04 06:30:39

标签: android validation saripaar

我在活动中成功使用了Saripaar Library 2.0。如何在Dialog中验证EditText。

我有一个登录界面,其中包含用户名,密码(EditTexts)和登录,忘记密码(按钮)。当我点击ForgotPassword。将出现对话框,它将要求发送电子邮件(EditText)

    public class LoginActivity extends AppCompatActivity implements Validator.ValidationListener, View.OnClickListener {

        @NotEmpty
        EditText username;

        @NotEmpty
        EditText password;

        private Button login;
        private Button forgotPassword;

        private Validator mValidator;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setTheme(android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);
            setContentView(R.layout.activity_login);
            init();

            mValidator = new Validator(this);
            mValidator.setValidationListener(this);

            forgotPassword.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Dialog dialog = new Dialog(LoginActivity.this);
                    dialog.setContentView(R.layout.activity_forgotpassword);
                    dialog.getWindow().setBackgroundDrawableResource(R.color.blue);
                    dialog.getWindow().setTitleColor(getResources().getColor(R.color.white));
                    dialog.setTitle("Forgot Password?");
                    EditText forgotpasswordrequest = (EditText) dialog.findViewById(R.id.forgotpasswordrequest);
                    Button request = (Button) dialog.findViewById(R.id.request);
                    request.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    });
                    dialog.show();
                }
            });
         login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mValidator.validate();
                }
            });
        @Override
        public void onValidationSucceeded() {
            startActivity(new Intent(LoginActivity.this, MenuActivity.class));
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }

        @Override
        public void onValidationFailed(List<ValidationError> errors) {
            for (ValidationError error : errors) {
                View view = error.getView();
                String message = error.getCollatedErrorMessage(this);
                if (view instanceof EditText) {
                    ((EditText) view).setError(message);
                } else {
                    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
                }
            }
        }

如何验证 EditText forgotpasswordrequest

如果我在登录活动中初始化了forgetpasswordrequest EditText

 @NotEmpty
    EditText forgotpasswordrequest;

运行时错误是:java.lang.IllegalStateException:&#39; EditText forgotpasswordrequest&#39;为空。

如果我在Dialog中初始化了forgetpasswordrequest EditText,则错误是,

@NotEmpty不适用于本地变量

如何在Dialog中单独验证EditText?

1 个答案:

答案 0 :(得分:-1)

请添加此行,然后重试,

EditText forgotpasswordrequest =(EditText)dialog.findViewById(R.id.forgotpasswordrequest);

String forgotpasswordreq = forgotpasswordrequest.getText().toString();