正在寻找有关TextUtils的一些建议和帮助消息显示在错误的EditText中。我正在检查所有的EditText,以确保它们没有被留空,并且该部分工作正常,它只是指向第一个EditText字段,而不是留空的正确字段,我想找出如何让它指向正确的字段。我已经将所有消息标记为EditText缺少的内容,正如您将从下面的屏幕截图中看到的那样,背景提示“输入值...”。为空,并且是自定义标签所指向的setError(),但是警告感叹号位于完全不同的EditText字段。我该怎么解决这个问题呢?我只是像这样调用setError()消息:someEditText.setError(custom message here)。谢谢大家。
请参见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. */发布于 2016-10-25 06:33:21
问题存在于用于调用setError(字符串消息)的编辑文本中
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());https://stackoverflow.com/questions/40227948
复制相似问题