我正在開發註冊功能,我需要在註冊之前做一些驗證。所以當我檢查是所有editText null它只是不起作用。我無法理解爲什麼。爲什麼editext getText不會返回任何內容?
這裏的代碼
public class RegisterActivity extends Activity implements View.OnClickListener {
private AlertDialogManager alert = new AlertDialogManager();
private EditText mFullName;
private EditText mEmail;
private EditText mPassword;
private EditText mPasswordAgain;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
init();
}
private void init() {
findViewById(R.id.registerButton).setOnClickListener(this);
mFullName = (EditText)findViewById(R.id.registerFullName);
mEmail = (EditText)findViewById(R.id.registerEmail);
mPassword = (EditText)findViewById(R.id.registerPassword);
mPasswordAgain = (EditText)findViewById(R.id.registerPasswordRetype);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.registerButton:
System.out.println("fullname="+mFullName.getText());
System.out.println("email="+mEmail.getText());
System.out.println("emailString="+mEmail.getText().toString());
if (mFullName.getText().toString().equals("") && mEmail.getText().toString().equals("") && mPassword.getText().toString().equals("") && mPasswordAgain.getText().toString().equals("")){
if (Utils.isValidEmail(mEmail.getText().toString())){
if (Utils.checkPassWordAndConfirmPassword(mPassword.getText().toString(), mPasswordAgain.getText().toString())){
register();
} else {
alert.showAlertDialog(RegisterActivity.this, "Error",
"Password doesn't match", false);
}
} else {
alert.showAlertDialog(RegisterActivity.this, "Error",
"Wrong email", false);
}
} else {
alert.showAlertDialog(RegisterActivity.this, "Error",
"All fields are required", false);
}
break;
}
}
}
你看有中的System.out.println點擊按鈕時。我也在這裏說,但是
08-01 11:34:46.830: INFO/System.out(10434): fullname=
08-01 11:34:46.830: INFO/System.out(10434): email=
08-01 11:34:46.830: INFO/System.out(10434): emailString=
如果getText是空的,它不打印null?
你應該使用gettext()。tostring()。 –
沒有試過它不起作用 – bauer9664
爲什麼你需要打印空?你可以這樣做: getText()。toString.length()> = 1或 if(getText()。toString()!=「」) - >如果在edittext中爲空文本。 你是第一個期待得到空! – Charan