我希望这是一个简单的解决方法。我无法弄清楚我在这里缺少什么。这是一个登录表单,我记得以前的登录电子邮件与共享偏好。 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{
答案 0 :(得分: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) {
}
});
}
}