2016-10-24 23 views
0

尋找一些建議和幫助TextUtils setError()消息顯示在錯誤的EditText中。我正在檢查EditText的所有內容,以確保它們不留空白,並且該部分正在工作,它只是指向第一個EditText字段,而不是正確的EditText字段,它留空,我想知道如何獲取它指向了正確的一個。我將所有標記爲EditText缺失的消息顯示在下面的屏幕截圖中,背景提示「輸入值...」爲空,這是自定義標籤指向的內容,但警告感嘆是一個完全不同的EditText字段。我該如何解決這個問題?我只是打電話給setError()這樣的消息: someEditText.setError(custom message here)。謝謝大家。TextUtils setError()顯示在錯誤的EditText字段

setError()截圖:

setError()

這裏是我使用的的EditText字段代碼。

/* Using the Pipe class setters to set the values collected from the editText fields. */ 
     /* 
     Implementing the TextUtils.isEmpty() method for all text fields that are Strings in nature 
     to check for empty values. Surrounding all those EditText fields that capture doubles with 
     a try/catch and calling a custom Toast message for them to the user. All of the messages 
     state exactly which EditText field was left empty. 
     */ 
     newPipe.setJointId(etPipeId.getText().toString()); 
     if (TextUtils.isEmpty((etPipeId.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+DISPLAY_PIPE_ID+" field"); 
      return; 
     } 

     newPipe.setJointNum(etPipeJtNum.getText().toString()); 
     if (TextUtils.isEmpty((etPipeJtNum.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+DISPLAY_PIPE_JT_NUM+" field"); 
      return; 
     } 

     newPipe.setJointHeat(etPipeHeat.getText().toString()); 
     if (TextUtils.isEmpty((etPipeHeat.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+DISPLAY_PIPE_HEAT+" field"); 
      return; 
     } 

     try { 
      newPipe.setJointLength(parseDouble(etPipeLt.getText().toString())); 
     }catch (Exception e) { 
      Toast.makeText(getApplicationContext() 
        ,EMPTY_VALUES+"\nPlease fill-in the "+DISPLAY_PIPE_LT+" field", 
        Toast.LENGTH_LONG).show(); 
      return; 
     } 

     newPipe.setJointManufacturer(etPipeManufacturer.getText().toString()); 
     if (TextUtils.isEmpty((etPipeManufacturer.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_JOINT_MANUFACTURER 
        +" field"); 
      return; 
     } 

     try { 
      newPipe.setWallThickness(parseDouble(etPipeWallThick.getText().toString())); 
     }catch (Exception e) { 
      Toast.makeText(getApplicationContext() 
        ,EMPTY_VALUES+"\nPlease fill-in the "+DISPLAY_JOINT_WALL_THICKNESS+" field", 
        Toast.LENGTH_LONG).show(); 
      return; 
     } 

     newPipe.setGrade(etPipeGrade.getText().toString()); 
     if (TextUtils.isEmpty((etPipeGrade.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_GRADE 
        +" field"); 
      return; 
     } 

     newPipe.setCoatingType(etPipeCoatType.getText().toString()); 
     if (TextUtils.isEmpty((etPipeCoatType.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_JOINT_COAT_TYPE 
        +" field"); 
      return; 
     } 

     newPipe.setCoatingThick(etPipeCoatThick.getText().toString()); 
     if (TextUtils.isEmpty((etPipeCoatThick.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_JOINT_COAT_THICKNESS 
        +" field"); 
      return; 
     } 

     try { 
      newPipe.setSizeDiameter(parseDouble(etPipeSizeDiameter.getText().toString())); 
     }catch (Exception e) { 
      Toast.makeText(getApplicationContext() 
        ,EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_JOINT_DIAMETER +" field", 
        Toast.LENGTH_LONG).show(); 
      return; 
     } 

     newPipe.setCurrentLocation(etPipeLocation.getText().toString()); 
     if (TextUtils.isEmpty((etPipeLocation.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_CURRENT_LOCATION 
        +" field"); 
      return; 
     } 

     try { 
      newPipe.setCuts(parseDouble(etPipeCuts.getText().toString())); 
     }catch (Exception e) { 
      Toast.makeText(getApplicationContext() 
        ,EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_CUTS +" field", 
        Toast.LENGTH_LONG).show(); 
      return; 
     } 

     try { 
      newPipe.setNewLength(parseDouble(etPipeNewLt.getText().toString())); 
     }catch (Exception e) { 
      Toast.makeText(getApplicationContext() 
        ,EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_NEW_LT +" field", 
        Toast.LENGTH_LONG).show(); 
      return; 
     } 

     newPipe.setChildJointId(etPipePup.getText().toString()); 
     if (TextUtils.isEmpty((etPipePup.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_CHILD_JT_ID 
        +" field"); 
      return; 
     } 

     newPipe.setNotes(etPipeNotes.getText().toString()); 
     if (TextUtils.isEmpty((etPipeNotes.getText().toString()))) { 
      etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+ DISPLAY_NOTES 
        +" field"); 
      return; 
     } 

     /* Inserting the collected data from above into the insertPipe() method*/ 
     if (dbHelper != null) { 
      dbHelper.insertPipe(newPipe); 
     } 
     finish(); 
     if (dbHelper != null) { 
      dbHelper.close(); /* closing the database down here to prevent resource leaks.*/ 
     } 

    } /* savePipe method ends here. */ 
+1

您可以加入一些代碼也許問題是有 –

回答

0

的問題在於編輯文本您正在使用的

newPipe.setJointId(etPipeId.getText().toString()); 
    if (TextUtils.isEmpty((etPipeId.getText().toString()))) { 
     etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+DISPLAY_PIPE_ID+" field"); 
     return; 
    } 

    newPipe.setJointNum(etPipeJtNum.getText().toString()); 
    if (TextUtils.isEmpty((etPipeJtNum.getText().toString()))) { 

     // You are using etPipeId instead of etPipeJtNum 
     etPipeId.setError(EMPTY_VALUES+"\nPlease fill-in the "+DISPLAY_PIPE_JT_NUM+" field"); 

     // Use this instead 
     etPipeJtNum.setError(EMPTY_VALUES+"\nPlease fill-in the "+DISPLAY_PIPE_JT_NUM+" field"); 

     return; 
    } 

希望這能解決您的問題,請致電SETERROR(字符串消息)。作爲意見要求

編輯

建議刪除重複的代碼,更容易調試。這是我如何寫我的代碼。可能有更好的方式來組織和構造的代碼,但我學習以及:)

private boolean checkEditTextIsEmpty(EditText et, String errorMessage){ 
    // Code Separate for easier reading 
    String text = et.getText().toString(); 

    if (TextUtils.isEmpty(text)) { 
     // Can consider using String.format(); 
     et.setError(EMPTY_VALUES+"\nPlease fill-in the "+errorMessage+" field"); 
     return true; 
    } 

    return false; 
} 

//This is your function that you called newPipe.setJointId 
boolean isIdEmpty = checkEditTextIsEmpty(etPipeId, DISPLAY_PIPE_ID); 
if (isIdEmpty) return; 

boolean isNumEmpty = checkEditTextIsEmpty(etPipeJtNum, DISPLAY_PIPE_JT_NUM); 
if (isNumEmpty) return; 

newPipe.setJointId(etPipeId.getText().toString()); 
newPipe.setJointNum(etPipeNum.getText().toString()); 
+0

OMG我覺得這麼愚蠢。當我將一個已完成的editText複製/粘貼到下一個並且未更正它所用的實際EditText時,這是我粗心大意的錯誤。由於etPipeId是第一個,所以我將它複製到其餘部分,甚至沒有抓到它。非常感謝。我的問題解決了。另一組眼睛不會受到傷害,我現在是一個堅定的信徒。再次感謝。 – J2112O

+0

哈哈,很高興我可以幫忙=)。我剛開始時也犯過類似的錯誤,我需要重複我的代碼。 如果它解決了您的問題,也許您可​​以將其標記爲問題,upvote並因此結束該問題? –

+0

我的一個建議是,您可能希望爲類似的錯誤檢查創建一個單獨的函數,在檢測到錯誤時引發EditText作爲方法參數以首先執行檢查,最後執行newPipe.setJointId()。 –

相關問題