在 Android Studio 中将一个类用于多个输入字段的快捷方式

我已经在一项活动(或一页......哈哈哈)中完成了注册、登录、重置密码布局和代码。他们都有电子邮件 editText e、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;

    }

}

如果您观察,每封电子邮件(e、e1 和 e2)的方法完全相同。问题是代码太多,我试图使代码尽可能少和短。我尝试创建一种方法来使用数组来处理此问题,但没有成功。请问我该如何使用一种方法来处理这些?请问有什么捷径吗?


红颜莎娜
浏览 77回答 1
1回答

婷婷同学_

使用一种方法进行所有电子邮件验证private boolean checkEmailValidation(EditText e) {&nbsp;&nbsp; &nbsp; String mail = e.getText().toString()&nbsp; &nbsp; if (mail.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; e.setError("Field cannot be empty");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){&nbsp; &nbsp; &nbsp; &nbsp; e.setError("Not a valid email");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; } else if (mail.length()>254) {&nbsp; &nbsp; &nbsp; &nbsp; e.setError("Email to long");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }else if (mail.length()<5) {&nbsp; &nbsp; &nbsp; &nbsp; e.setError("Email too short");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; e.setError(null);&nbsp; &nbsp; &nbsp; &nbsp; // e.setErrorEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp;}现在您可以checkEmailValidation()对所有电子邮件使用该方法。// you can check all email like following&nbsp;if(checkEmailValidation(e.getEditText()) && checkEmailValidation(e1.getEditText()) && checkEmailValidation(e2.getEditText())) {&nbsp; &nbsp; // do whatever you want here when all email is ok}else{&nbsp; // ...}要多次使用,activities您可以遵循两种方式创建一个BaseActivity并将其扩展为 all&nbsp;activity。创建一个class并创建一个static方法。基本活动示例public abstract class BaseActivity extends AppCompatActivity {&nbsp; private boolean checkEmailValidation(EditText e) {&nbsp;&nbsp; &nbsp; String mail = e.getText().toString()&nbsp; &nbsp; if (mail.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; e.setError("Field cannot be empty");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){&nbsp; &nbsp; &nbsp; &nbsp; e.setError("Not a valid email");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; } else if (mail.length()>254) {&nbsp; &nbsp; &nbsp; &nbsp; e.setError("Email to long");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }else if (mail.length()<5) {&nbsp; &nbsp; &nbsp; &nbsp; e.setError("Email too short");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; e.setError(null);&nbsp; &nbsp; &nbsp; &nbsp; // e.setErrorEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; }}BaseActivity并在子项中扩展activities如下public class ChildActivity extends BaseActivity{&nbsp;// within this class you can use checkEmailValidation`}静态函数示例public class YourClassName{&nbsp; &nbsp;private static boolean checkEmailValidation(EditText e) {&nbsp;&nbsp; &nbsp; &nbsp; String mail = e.getText().toString()&nbsp; &nbsp; &nbsp; if (mail.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.setError("Field cannot be empty");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; } else if (!Patterns.EMAIL_ADDRESS.matcher(mail).matches()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.setError("Not a valid email");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; } else if (mail.length()>254) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.setError("Email to long");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; }else if (mail.length()<5) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.setError("Email too short");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.setError(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // e.setErrorEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}现在您可以method使用class name如下方式调用它public class YourActivity extends AppCompatActivity{&nbsp; @Override&nbsp; protected void onCreate(@Nullable Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(getContentView());&nbsp; &nbsp; // you can use checkEmailValidation like&nbsp;&nbsp; &nbsp; YourClassName.checkEmailValidation(...)&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java