TextUtils setError()显示在错误的EditText字段中

时间:2016-10-24 21:35:23

标签: java android

寻找一些建议和TextUtils setError()消息的帮助显示在错误的EditText中。我正在检查所有的EditText以确保它们不会留空并且该部分正在工作,它只是指向第一个EditText字段而不是正确的那个留空并且我想知道如何获取它指向正确的一个。我将消息全部标记为EditText丢失的内容,您将从下面的屏幕截图中看到,背景提示“输入值...”为空,是setError()指向自定义标签的内容,但是警告感叹号是一个完全不同的EditText字段。我怎么能解决这个问题?我只是像这样调用setError()消息:  someEditText.setError(custom message here)。谢谢大家。

请参阅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 :(得分:0)

问题出在编辑文本,用于调用

上的setError(String message)
    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;
    }

希望这能解决你的问题。

修改

根据评论中的要求删除重复代码和更轻松调试的建议。这就是我编写代码的方式。可能有更好的方法来组织和构建代码,但我也在学习:)

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());