Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android Studio中使用一个类处理多个输入字段的捷径_Java_Arrays_Android Studio - Fatal编程技术网

Java 在Android Studio中使用一个类处理多个输入字段的捷径

Java 在Android Studio中使用一个类处理多个输入字段的捷径,java,arrays,android-studio,Java,Arrays,Android Studio,我在一个活动(或一个页面)中注册、登录、重置密码布局和代码。它们都有电子邮件文本edite、e1和e2。现在,我为每种方法创建了一个方法,如下所示: private boolean valEmail() { String mail = e.getEditText().getText().toString().trim(); if (mail.isEmpty()) { e.setError("Field cannot be empty"); ret

我在一个活动(或一个页面)中注册、登录、重置密码布局和代码。它们都有电子邮件文本edite、e1和e2。现在,我为每种方法创建了一个方法,如下所示:

private boolean valEmail() {

    String mail = e.getEditText().getText().toString().trim();
    if (mail.isEmpty()) {
        e.setError("Field cannot be empty");
        return false;
    } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){
        e.setError("Not a valid email"); return false;
    } else if (mail.length()>254) {e.setError("Email to long"); return false;}
    else if (mail.length()<5) {e.setError("Email too short"); return false;}
    else {
        e.setError(null);
        // e.setErrorEnabled(false);
        return true;
    }
}

private boolean valEmail1() {

    String mail = e1.getEditText().getText().toString().trim();

    if (mail.isEmpty()) {
        e.setError("Field cannot be empty");
        return false;
    } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){
        e.setError("Not a valid email"); return false;
    } else if (mail.length()>254) {e.setError("Email to long"); return false;}
    else if (mail.length()<5) {e.setError("Email too short"); return false;}
    else {
        e.setError(null);
        // e.setErrorEnabled(false);
        return true;
    }
}

private boolean valEmail2() {

    String mail = e2.getEditText().getText().toString().trim();

    if (mail.isEmpty()) {
        e.setError("Field cannot be empty");
        return false;
    } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){
        e.setError("Not a valid email"); return false;
    } else if (mail.length()>254) {e.setError("Email to long"); return false;}
    else if (mail.length()<5) {e.setError("Email too short"); return false;}
    else {
        e.setError(null);
        // e.setErrorEnabled(false);
        return true;
    }
}
private boolean valEmail(){
字符串mail=e.getEditText().getText().toString().trim();
if(mail.isEmpty()){
e、 setError(“字段不能为空”);
返回false;
}else如果(!Patterns.EMAIL\u ADDRESS.matcher(mail.matches()){
e、 setError(“无效电子邮件”);返回false;
}else if(mail.length()>254){e.setError(“Email to long”);返回false;}
else if(mail.length()254){e.setError(“Email to long”);返回false;}
else if(mail.length()254){e.setError(“Email to long”);返回false;}

else if(mail.length()对所有电子邮件验证使用一种方法

private boolean checkEmailValidation(EditText e) { 
    String mail = e.getText().toString()
    if (mail.isEmpty()) {
        e.setError("Field cannot be empty");
        return false;
    } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){
        e.setError("Not a valid email"); 
        return false;
    } else if (mail.length()>254) {
        e.setError("Email to long");
        return false;
    }else if (mail.length()<5) {
        e.setError("Email too short");
        return false;
    }else {
        e.setError(null);
        // e.setErrorEnabled(false);
        return true;
    }
 }
要在多个
活动中使用
,您可以遵循两种方法

  • 创建一个
    BaseActivity
    并在所有
    活动中扩展它
  • 创建一个
    并创建一个
    静态
    方法
  • 基本活动示例

    public abstract class BaseActivity extends AppCompatActivity {
    
      private boolean checkEmailValidation(EditText e) { 
        String mail = e.getText().toString()
        if (mail.isEmpty()) {
            e.setError("Field cannot be empty");
            return false;
        } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){
            e.setError("Not a valid email"); 
            return false;
        } else if (mail.length()>254) {
            e.setError("Email to long");
            return false;
        }else if (mail.length()<5) {
            e.setError("Email too short");
            return false;
        }else {
            e.setError(null);
            // e.setErrorEnabled(false);
            return true;
        }
      }
    }
    
    public class YourClassName{
       private static boolean checkEmailValidation(EditText e) { 
          String mail = e.getText().toString()
          if (mail.isEmpty()) {
              e.setError("Field cannot be empty");
              return false;
          } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){
              e.setError("Not a valid email"); 
              return false;
          } else if (mail.length()>254) {
              e.setError("Email to long");
              return false;
          }else if (mail.length()<5) {
              e.setError("Email too short");
              return false;
          }else {
              e.setError(null);
              // e.setErrorEnabled(false);
              return true;
          }
       }
    }
    
    静态功能示例

    public abstract class BaseActivity extends AppCompatActivity {
    
      private boolean checkEmailValidation(EditText e) { 
        String mail = e.getText().toString()
        if (mail.isEmpty()) {
            e.setError("Field cannot be empty");
            return false;
        } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){
            e.setError("Not a valid email"); 
            return false;
        } else if (mail.length()>254) {
            e.setError("Email to long");
            return false;
        }else if (mail.length()<5) {
            e.setError("Email too short");
            return false;
        }else {
            e.setError(null);
            // e.setErrorEnabled(false);
            return true;
        }
      }
    }
    
    public class YourClassName{
       private static boolean checkEmailValidation(EditText e) { 
          String mail = e.getText().toString()
          if (mail.isEmpty()) {
              e.setError("Field cannot be empty");
              return false;
          } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){
              e.setError("Not a valid email"); 
              return false;
          } else if (mail.length()>254) {
              e.setError("Email to long");
              return false;
          }else if (mail.length()<5) {
              e.setError("Email too short");
              return false;
          }else {
              e.setError(null);
              // e.setErrorEnabled(false);
              return true;
          }
       }
    }
    

    保持它们分开不需要在一个活动中把整件事情弄得乱七八糟。很漂亮。这正是我要找的。非常简短。用于e、e1、e2等的“编辑e”。非常感谢。我会尝试一下。谢谢。我将字符串改为布尔值。即从“私有字符串checkEmailValidation”改为“私有布尔值checkEmailValidation”它工作起来很有魅力。谢谢。如果我要创建一个类(在另一个页面中)并添加checkEmailValidation()代码,我如何在多个活动(多个页面)中使用或调用它。我是android编程新手。。。。
    public class YourActivity extends AppCompatActivity{
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
    
        // you can use checkEmailValidation like 
        YourClassName.checkEmailValidation(...)
    
      }
    
    }