2016-06-07 95 views
-3

我有一個名爲移動和密碼的編輯文本字段,情景是,當應用程序啓動它時出現錯誤文本,當我在相應的字段中輸入文本時,並且在完成編輯文本後檢查值如果正確,然後移動其他顯示該領域的錯誤。如何在android中輸入文本時在編輯文本中檢查值?

代碼: -

private final BroadcastReceiver m_oOtpReceiver = new BroadcastReceiver() {// creating broadcast to receive otp sent by server from Inbox... 
    @Override 
    public void onReceive(Context context, Intent intent) {// on receive method to read OTP sent by server 
     checkFieldsForEmpty(true);// check whether edit text is empty or not 

    } 
}; 
private TextWatcher m_oTextWatcher = new TextWatcher() {// making object of TextWathcher class 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) {// when text change in Edit tEXT 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     checkFieldsForEmpty(false);// CHECK LOGIN BUTTON DISABLED AND ENABED 
    } 
}; 

/*This method check Edit text is empty or not*/ 
public void checkFieldsForEmpty(boolean fromBroadcast) {// this method check Edit text is empty or not 

    s_szMobileNumber = m_InputMobile.getText().toString().trim();// get mobile number from edit Text 
    s_szPassword = m_InputPassword.getText().toString().trim();// get password from edit text 

    if (NetworkUtil.isConnected(getApplicationContext())) { 
     // if mobile number and password are Emoty 
     if (s_szMobileNumber != null && s_szMobileNumber.length() > 7 && s_szMobileNumber.length() < 15) {// check if mobile and password is empty .. 
      if (s_szPassword.length() >= 4 && s_szPassword.length() <= 8) { 
       m_LoginBtn.setEnabled(true);// make Login button disabled 

       m_LoginBtn.setBackgroundColor(Color.rgb(0, 80, 147));// set background color on eabled 
       m_LoginBtn.setOnClickListener(new View.OnClickListener() {// onclick listener on Login Button 
        @Override 
        public void onClick(View v) { 
         postLoginDataToServer(); 

        } 
       }); 
      } else { 
        if (!fromBroadcast) { 

         m_InputPassword.setError("Password must be between 4 to 8 characters long"); 
        } 
       m_LoginBtn.setEnabled(false);// make login button enabled 
       m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));// color of login button 
      } 

     } else { 
       if (!fromBroadcast) { 
        m_InputMobile.setError("Mobile number must be between 7 to 15 characters long"); 
       } 

      m_LoginBtn.setEnabled(false);// make login button enabled 
      m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));// color of login button 

     } 

    } else { 
     try { 
      CSnackBar.getInstance().showSnackBarError(findViewById(R.id.mainLayout), "No Internet Connection Available", getApplicationContext()); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     m_LoginBtn.setEnabled(false); 
     m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192)); 
    } 


} 

回答

0

我看不到的地方布爾「fromBroadcast」或者是初始化的或獲取它從知道狀態是否是你需要設置錯誤消息之前檢查什麼在textView上。從我讀過的內容中,我會假設你不需要使用if語句來檢查「fromBroadcast」。代碼應該沒有它。如果您使用錯誤消息啓動應用程序,並且您不想要錯誤消息,那麼您應該檢查s_szPassword而不是m_InputPassword進行輸入,並且可以初始化s_szPassword以首先包含正確的消息,以便錯誤消息在開始時不存在。

+0

請更新代碼 – Nitin

相關問題