0
我想在提交之前驗證表單中的用戶輸入,但它允許所有空字段經過...任何想法?我想 「」,而不是零,以及...Java EditText驗證不起作用
public void onClick(View v) {
EditText agencyname = (EditText) findViewById(R.id.agencyname);
String agency = agencyname.getText().toString();
EditText firstname = (EditText) findViewById(R.id.firstname);
String first = firstname.getText().toString();
EditText lastname = (EditText) findViewById(R.id.lastname);
String last = lastname.getText().toString();
EditText phone = (EditText) findViewById(R.id.phone);
String agencyphone = phone.getText().toString();
EditText email = (EditText) findViewById(R.id.email);
String agencyemail = email.getText().toString();
if(agency != null || first != null || last != null || agencyphone != null || agencyemail != null){
Intent i = new Intent();
i.setClassName("android.com.smartchoice", "android.com.smartchoice.AgencyRecieve");
i.putExtra("agencyname", agency);
i.putExtra("phone", agencyphone);
i.putExtra("email", agencyemail);
i.putExtra("firstname", first);
i.putExtra("lastname", last);
startActivity(i);
}
else
{
Toast.makeText(NewAgencyActivity.this, "Must Input All Fields", Toast.LENGTH_LONG).show();
};
}
當我嘗試isEmpty()是說方法是未定義的。我試着agency.length()== 0,它仍然讓我提交空字段。 – savagenoob
根據http://developer.android.com/reference/java/lang/String.html#isEmpty%28%29,應該不是這樣,儘管'agency.length()== 0'應該可以工作以及。你確定你正在使用'&&'? –
是的,我改成了&&,我弄明白了,我是用==代替!= – savagenoob