0

我在活动中成功使用了 Saripaar Library 2.0。如何验证对话框内的 EditText。

我有一个带有用户名、密码(EditTexts)和登录、忘记密码(按钮)的登录屏幕。当我单击忘记密码时。将出现对话框并询问电子邮件(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

如果我在登录活动中初始化忘记密码请求 EditText

 @NotEmpty
    EditText forgotpasswordrequest;

运行时错误是:java.lang.IllegalStateException: 'EditText forgotpasswordrequest' 为空。

如果我在对话框中初始化 forgotpasswordrequest EditText,错误是,

@NotEmpty 不适用于局部变量

如何单独验证 Dialog 内的 EditText?

4

1 回答 1

-1

请添加此行并重试,

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

String forgotpasswordreq = forgotpasswordrequest.getText().toString();
于 2016-04-04T06:38:58.603 回答