亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Android Studio 中將一個類用于多個輸入字段的快捷方式

在 Android Studio 中將一個類用于多個輸入字段的快捷方式

紅顏莎娜 2023-09-27 15:14:15
我已經在一項活動(或一頁......哈哈哈)中完成了注冊、登錄、重置密碼布局和代碼。他們都有電子郵件 editText e、e1 和 e2?,F在我為每個方法創建了一個方法,如下所示: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)的方法完全相同。問題是代碼太多,我試圖使代碼盡可能少和短。我嘗試創建一種方法來使用數組來處理此問題,但沒有成功。請問我該如何使用一種方法來處理這些?請問有什么捷徑嗎?
查看完整描述

1 回答

?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

使用一種方法進行所有電子郵件驗證


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;

    }

 }

現在您可以checkEmailValidation()對所有電子郵件使用該方法。



// you can check all email like following 

if(checkEmailValidation(e.getEditText()) && checkEmailValidation(e1.getEditText()) && checkEmailValidation(e2.getEditText())) {

    // do whatever you want here when all email is ok

}else{

  // ...

}

要多次使用,activities您可以遵循兩種方式

  1. 創建一個BaseActivity并將其擴展為 all activity。

  2. 創建一個class并創建一個static方法。

基本活動示例

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;

    }

  }

}

BaseActivity并在子項中擴展activities如下


public class ChildActivity extends BaseActivity{

 // within this class you can use checkEmailValidation`

}

靜態函數示例


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;

      }

   }

}

現在您可以method使用class name如下方式調用它


public class YourActivity extends AppCompatActivity{

  @Override

  protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(getContentView());


    // you can use checkEmailValidation like 

    YourClassName.checkEmailValidation(...)


  }


}



查看完整回答
反對 回復 2023-09-27
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號