0

我希望这是一个简单的解决方法。我无法弄清楚我在这里缺少什么。这是一个登录表单,我让它记住了以前带有共享首选项的登录电子邮件。editText 框在 onCreate 中预填充了电子邮件,但 onClick 认为 editText 字段为空。有任何想法吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    setContentView(R.layout.main);

    email = (EditText) findViewById(R.id.login_email_address);
    pass = (EditText) findViewById(R.id.password);

    login = (Button) findViewById(R.id.blogin);
    text = (TextView) findViewById(R.id.invalid_email);


    // register listeners
    newuser.setOnClickListener(this);
    login.setOnClickListener(this);
    app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

    String restoredemail = app_preferences.getString("username", null);
    if (restoredemail != null) {
        email.setText(restoredemail);

    email.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            if (email.getText().toString()
                    .matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")
                    && s.length() > 0) {
                text.setText("valid email");
                valid_email = true;
                //user_email = email.toString();
            } else {
                text.setText("invalid email");
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
    });
}

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.blogin:
        if (valid_email == true) {
            new AttemptLogin().execute();   
        }else{
4

1 回答 1

1

好的,我明白了。

我有一个 addTextChangedListener 来检查在提交之前是否输入了有效的电子邮件。由于文本没有改变(正在输入)它没有注册。我不知道这是否会在某天对某人有所帮助,但这里是修改后的代码。感谢所有发布帮助的人。

String restoredemail = app_preferences.getString("username", null);
    if (restoredemail != null) {
        email.setText(restoredemail);
        valid_email = true;
        text.setText("valid email");
    }else{  

        email.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            if (email.getText().toString()
                    .matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")
                    && s.length() > 0) {
                text.setText("valid email");
                valid_email = true;
                //user_email = email.toString();
            } else {
                text.setText("invalid email");
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
    });
}

}
于 2013-08-29T22:26:26.233 回答