Java TextUtils setError()显示在错误的EditText字段中
正在查找有关text的建议和帮助utilsJava TextUtils setError()显示在错误的EditText字段中,java,android,Java,Android,正在查找有关text的建议和帮助utilssetError()消息显示在错误的编辑文本中。我正在检查所有的EditText,以确保它们没有留空,并且该部分正在工作,它只是指向第一个EditText字段,而不是留空的正确字段,我想了解如何使它指向正确的字段。我将所有消息标记为EditText缺失的内容,正如您将从下面的屏幕截图中看到的,背景提示“Input Value…”为空,并且是自定义标签所指向的setError(),但警告感叹号位于完全不同的EditText字段。我怎样才能解决这个问题?我只
setError()
消息显示在错误的编辑文本中。我正在检查所有的EditText,以确保它们没有留空,并且该部分正在工作,它只是指向第一个EditText字段,而不是留空的正确字段,我想了解如何使它指向正确的字段。我将所有消息标记为EditText缺失的内容,正如您将从下面的屏幕截图中看到的,背景提示“Input Value…”为空,并且是自定义标签所指向的setError()
,但警告感叹号位于完全不同的EditText字段。我怎样才能解决这个问题?我只是这样调用setError()
消息:
someEditText.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. */
问题在于编辑文本您用来调用上的setError(字符串消息) 希望这能解决你的问题 编辑 根据评论中的要求,建议删除重复代码并简化调试。这就是我编写代码的方式。也许有更好的方法来组织和构造代码,但我也在学习:)
问题在于编辑文本您用来调用上的setError(字符串消息) 希望这能解决你的问题 编辑 根据评论中的要求,建议删除重复代码并简化调试。这就是我编写代码的方式。也许有更好的方法来组织和构造代码,但我也在学习:)
你能添加一些代码吗?也许问题是,你能添加一些代码吗?也许问题是,我觉得很愚蠢。这是我在将一个已完成的编辑文本复制/粘贴到下一个编辑文本时的粗心错误,并且没有更正它用于哪个实际编辑文本。由于etPipeId是第一个,我把它复制到了其他人身上,甚至都没有抓到它。非常感谢。我的问题解决了。我现在是一个坚定的信仰者。再次感谢。哈哈,很高兴我能帮上忙。我在过去的起步阶段也犯过类似的错误,我需要重复我的代码。如果它解决了您的问题,也许您可以将其标记为QUISE、upvote,从而结束问题?我的建议是,您可能希望为类似的错误检查创建一个单独的函数,将EditText作为方法参数,首先执行检查,最后执行newPipe.setJointId()当没有检测到错误时。这是个好主意。再次感谢您的帮助。我对Android和Java还比较陌生,那么您将如何着手实现类似的东西呢?一个粗略的例子可以帮助我理解它。或者更好的是,给我指一些资源来学习如何做到这一点。我可以在哪里学习?再次感谢你的帮助。天哪,我觉得自己太蠢了。这是我在将一个已完成的编辑文本复制/粘贴到下一个编辑文本时的粗心错误,并且没有更正它用于哪个实际编辑文本。由于etPipeId是第一个,我把它复制到了其他人身上,甚至都没有抓到它。非常感谢。我的问题解决了。我现在是一个坚定的信仰者。再次感谢。哈哈,很高兴我能帮上忙。我在过去的起步阶段也犯过类似的错误,我需要重复我的代码。如果它解决了您的问题,也许您可以将其标记为QUISE、upvote,从而结束问题?我的建议是,您可能希望为类似的错误检查创建一个单独的函数,将EditText作为方法参数,首先执行检查,最后执行newPipe.setJointId()当没有检测到错误时。这是个好主意。再次感谢您的帮助。我对Android和Java还比较陌生,那么您将如何着手实现类似的东西呢?一个粗略的例子可以帮助我理解它。或者更好的是,给我指一些资源来学习如何做到这一点。我可以在哪里学习?再次感谢你的帮助。
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());