我试图编写一个简单的注册应用程序,用户必须填写一些字段,其中一些字段是强制性的。所以,我需要检查用户是否在那里写信息。我这样做:
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;
}
}
}
boolValue()返回TextUtils.isEmpty(Field)的状态,但是这部分我不知道怎么做,现在的RequireFileds是一个枚举:
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;
}
}
我输入了一个带有名字的数字,所以我可以使用迭代。 任何想法或简单的方法吗?
PD:这是我在其他应用上使用的两个字段的代码:
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;
}
答案 0 :(得分:2)
我这样做是为了检查空的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
}
希望这有帮助..
答案 1 :(得分:1)
我没有抓住你想要的东西。以下是我用过的内容
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:
....
}
希望这个帮助