问题
I'm trying to program a simple register application, the user have to fill some fields, where some of them are obligatory. So, I need to check if the user write the info there. I'm doing this:
for(RequireFields r : RequireFields.values()){
if(boolValue()){
switch (r.getField()) {
case "name":
System.out.println("Name");
break;
case "surname":
System.out.println("Surname");
break;
case "pass":
System.out.println("Pass");
break;
case "email":
System.out.println("Email");
break;
case "street":
System.out.println("Street");
break;
case "city":
System.out.println("City");
break;
case "zip":
System.out.println("Zip");
break;
}
}
}
Where boolValue() return the status of TextUtils.isEmpty(Field), but this part I don't know how to do it, RequireFileds for now is an enum:
public enum RequireFields {
NAME("name",1),
SURNAME("surname",2),
EMAIL("email",3),
PASS("pass",4),
CITY("city",5),
STREET("street",6),
ZIP("zip",7);
private final String field;
private final int position;
RequireFields(String field, int position) {
this.field = field;
this.position = position;
}
public String getField() {
return field;
}
public int getPosition() {
return position;
}
}
I put a number with the name, so I can use an iterate for. Any ideas or a easy way to do this?
PD: This is the code for two fields that I used on other app:
ssid.setError(null);
pass.setError(null);
String SSID = ssid.getText().toString();
String PASS = pass.getText().toString();
boolean cancel = false;
View focusView = null;
if (TextUtils.isEmpty(PASS) && !openWifi) {
pass.setError(getString(R.string.error_field_pass));
focusView = pass;
cancel = true;
}
if (TextUtils.isEmpty(SSID)) {
ssid.setError(getString(R.string.error_field_ssid));
focusView = ssid;
cancel = true;
}
回答1:
I do this for checking empty EditText...
if (editTextName.getText().toString().equalsIgnoreCase(""))
{
// show message/toast for empty name
}
else if (editTextSurname.getText().toString().equalsIgnoreCase(""))
{
// show message/toast for empty Surname
}
else if (editTextEmail.getText().toString().equalsIgnoreCase(""))
{
// show message/toast for empty Email
}
else
{
// If above all conditions are false means all fields are not empty so put your action here
}
Hope this help..
回答2:
I am not catch what you want. Below is what I had used
private static final SparseArray< RequireFields > _map=new SparseArray< RequireFields >();
static {
for (RequireFields cmd: RequireFields.values())
{
_map.put(cmd.position,cmd);
}
}
public static RequireFields valueOf(int value) {
RequireFields field = _map.get(value);
if (field==null) {
Log.d("RequireFields","RequireFields position "+value+" not found");
}
return field;
}
switch(RequireFields.valueOf(value){
case NAME:
....... // your handler
break;
case SURNAME:
....
}
Hope this help
来源:https://stackoverflow.com/questions/30029257/checking-empty-textedit