2014-11-24 57 views
0

我正在嘗試配置一個簡單的窗體。我有3個editexts,並在用戶結束輸入後,我想顯示檢查圖標,如果它是有效的(並進入下一個領域)和錯誤,如果不是,用戶可以更正。在TextWatcher中使用edittext問題

我的主要問題是我只能輸入1個字符,然後顯示我或錯誤或檢查圖標,它會轉到下一個字段。

我該怎麼做?

這裏是我的代碼:

public class ConTct extends Activity implements OnClickListener, OnTouchListener{ 

    Button mButton; 
    EditText mFullName, mEmail, mDialZone, mPhone; 
    static WebView mWebView; 
    static ProgressBar mProgressBar; 
    EditText mBrokerId, mIP, mAreaPhonePrefix, mLastName, mPassWord, mCampaign, mSubCampaign, mCountryID, mCity, mAdress, mIsDemo; 

    private String inputText; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.contct); 

     registerViews(); 


    }//oncreate() 


     private void registerViews(){ 
    mFullName = (EditText) findViewById(R.id.firstName); 
    mFullName.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      // TODO Auto-generated method stub 
      if(hasFocus){ 
       //do nothing 
      }else{ 
       if(!Validation.hasText(mFullName)){ 
        mFullName.setError("You have to input at least 3 characters"); 
        mFullName.requestFocus(); 
       }else{ 
        mFullName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.llgr, 0); 
        mFullName.clearFocus(); 

        mEmail.requestFocus(); 
       } 
      } 
     } 
    }); 

    mEmail = (EditText) findViewById(R.id.email); 

    mEmail.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      // TODO Auto-generated method stub 
      if(hasFocus){ 
       //do nothing 
      }else{ 

       if(!Validation.isEmailAddress(mEmail, true)){ 
        mEmail.setError("wrong email"); 
        mEmail.requestFocus(); 
       }else{ 
        mEmail.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.llgr, 0); 
        mEmail.clearFocus(); 

        mPhone.requestFocus(); 
       } 

      } 
     } 
    }); 
    mPhone = (EditText) findViewById(R.id.phoneNumber); 

    mPhone.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      // TODO Auto-generated method stub 

      // TODO Auto-generated method stub 
      if(hasFocus){ 
       //do nothing 
      }else{ 

       if(!Validation.isPhoneNumber(mPhone, true)){ 
        mPhone.setError("wrong email"); 
        mPhone.requestFocus(); 
       }else{ 
        mPhone.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.llgr, 0); 
        mPhone.clearFocus(); 


       } 

      } 

     } 
    }); 

     mButton = (Button) findViewById(R.id.SubmitRegisterButton); 
     mButton.setOnClickListener(this); 
    } 


    private void submitForm() { 
     // Submit your form here. your form is valid 
     Toast.makeText(this, "Submitting form...", Toast.LENGTH_LONG).show(); 
    } 

    private boolean checkValidation() { 
     boolean ret = true; 

     if (!Validation.hasText(mFullName)){ 
      ret = false; 

     } 

     if (!Validation.isEmailAddress(mEmail, true)){ 
      ret = false; 
     } 

     if (!Validation.isPhoneNumber(mPhone, true)){ 
      ret = false; 
     } 

     if (!Validation.hasText(mPhone)){ 

      ret = false; 

     } 

     return ret; 
    }//checkValidation 


    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 


     if (checkValidation()){ 
      submitForm(); 
      Intent i = new Intent(getApplicationContext(), ThanksZuser.class); 
      startActivity(i); 
      finish(); 
      //send to our crm 
      grabURL(myURL); 
     }else 
      Toast.makeText(ConTct.this, "Form contains error", Toast.LENGTH_LONG).show(); 
    } 
}//ConTct.class 

驗證類:

package com.Signals4Trading.push.android; 

import java.util.regex.Pattern; 

import android.widget.EditText; 

public class Validation { 

    // Regular Expression 
    // you can change the expression based on your need 
    private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
    // private static final String PHONE_REGEX = "\\d{3}-\\d{7}"; 
    // private static final String PHONE_REGEX = "(\\d{3})-(\\d{3})-(\\d{4})"; 
    private static final String PHONE_REGEX = "([0-9-()]+)"; 


    // Error Messages 
    private static final String REQUIRED_MSG = "Invalid name. You have to input at least 3 characters"; 
    private static final String EMAIL_MSG = "Invalid email"; 
    private static final String PHONE_MSG = "Invalid number"; 

    // call this method when you need to check email validation 
    public static boolean isEmailAddress(EditText editText, boolean required) { 
     return isValid(editText, EMAIL_REGEX, EMAIL_MSG, required); 
    } 

    // call this method when you need to check phone number validation 
    public static boolean isPhoneNumber(EditText editText, boolean required) { 
     return isValid(editText, PHONE_REGEX, PHONE_MSG, required); 
    } 

    /* 
    // return true if the input field is valid, based on the parameter passed 
    public static boolean isValid(EditText editText, String regex, String errMsg, boolean required) { 

     String text = editText.getText().toString().trim(); 
     // clearing the error, if it was previously set by some other values 
     editText.setError(null); 

     // text required and editText is blank, so return false 
     if (required && !hasText(editText)) return false; 

     // pattern doesn't match so returning false 
     if (required && !Pattern.matches(regex, text)) { 
      editText.setError(errMsg); 
      return false; 
     }; 

     if (required && !hasNumber(editText)) return false; 

     return true; 
    } 
    */ 

    // return true if the input field is valid, based on the parameter passed 
    public static boolean isValid(EditText editText, String regex, String errMsg, boolean bRequired) { 
     // text required and editText is blank, so return false 
     String sText = editText.getText().toString().trim(); 
     // clearing the error, if it was previously set by some other values 
     editText.setError(null); 
     if (sText.length() == 0) { 
      if (bRequired) { 
       editText.setError("*Field required"); 
       return false; 
      } 
     } else { 
      // filled field 
      // pattern doesn’t match so returning false 
      if (!Pattern.matches(regex, sText)) { 
       editText.setError(errMsg); 
       return false; 
      } 
     } 
     return true; 
    } 

    // check the input field has any text or not 
    // return true if it contains text otherwise false 
    public static boolean hasText(EditText editText) { 

     String text = editText.getText().toString().trim(); 
     editText.setError(null); 

     // length less that 3 means there is no text 
     if (text.length() <= 3 && text.length() > 12) { 
      editText.setError(REQUIRED_MSG); 
      return false; 
     } 

     return true; 
    } 

    public static boolean hasNumber(EditText editText) { 

     String text = editText.getText().toString().trim(); 
     editText.setError(null); 

     // length less that 6 and more than 11 means not available 
     if (text.length() <= 6 && text.length() >= 11) { 
      editText.setError(PHONE_MSG); 
      return false; 
     } 

     return true; 
    } 

}//Validation 

回答

1

我的主要問題是,我只能輸入一個字符,然後它表明我或 錯誤或檢查圖標,它進入下一個領域。

的問題是:

您調用onTextChanged的驗證方法。所以它進入hasText方法,確定有少於3個字符,並顯示錯誤。在你的情況下,它能夠更好地實現使用OnFocusChangeListener

例子:

myEditText.setOnFocusChangeListener(new OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if(hasFocus){ 
      //do nothing 
     }else { 
      // validate here and show error if invalid and set focus to this element again using requestFocus. 
     } 
    } 
}); 

希望這有助於。

+0

哦,謝謝你!我編輯我的文本,並試圖做你的解釋。現在光標正在第一個編輯文本和第二個編輯文本之間進行「乒乓」操作。當我輸入一些文本後,我想要進入第二個字段,並且光標從一個字段轉到另一個字段,而無需我做任何事情。我做錯了什麼? – user3111349 2014-11-24 08:58:24

+0

@ user3111349我沒有明白你在說什麼。 – 2014-11-24 09:04:56

+0

它不工作,因爲它應該..我的編輯代碼看起來好嗎?或者我做錯了什麼? – user3111349 2014-11-24 09:09:36

相關問題