[英]Android EditText setEnabled(false) isn't working
我有几个动态创建的 EditText 字段。 如果另一个字段中的值与某些条件不匹配,我将尝试禁用这些字段。 问题是,如果满足其他条件,则通过设置setEnabled(false);
正确禁用该字段setEnabled(false);
.
基本上在零件编号字段中,我们检查零件编号是否需要序列号或批号。 如果零件需要序列号,序列号字段显示错误,批号字段禁用,数量字段设置为“1”并禁用 如果需要批号,批号字段显示错误和序列号字段被禁用。 这按预期工作。
public void onTextChanged(@NotNull EditText target, @NotNull Editable s) {
int serialIndex = 4;
int lotIndex = 5;
int quantityIndex = 6;
EditText serial = findViewById(serialIndex);
EditText lot = findViewById(lotIndex);
EditText quantity = findViewById(quantityIndex);
String txt = partNum.getText().toString().toUpperCase();
partNumber = txt;
Global.isSerialized = DatabaseManager.isSerialized(txt);
Global.isLot = DatabaseManager.isLot(txt);
//this is not working. The fields do not get disabled.
if (!Global.isSerialized && !Global.isLot) {
lot.setEnabled(false);
serial.setEnabled(false);
findViewById(id).setNextFocusDownId(quantityIndex);
}
if (txt.equals("")) {
serial.setError(null);
lot.setError(null);
if (!quantity.isEnabled()) {
quantity.setEnabled(true);
quantity.setText("");
}
}
//This is working. The lot number field gets disabled.
if (Global.isSerialized) {
lot.setEnabled(false);
snRequired = true;
serial.setError("Serial Number Required");
quantity.setText("1");
quantity.setEnabled(false);
findViewById(id).setNextFocusDownId(serialIndex);
} else {
snRequired = false;
serial.setError(null);
quantity.setText("");
quantity.setEnabled(true);
}
//this is also working. The serial number field gets disabled.
if (Global.isLot) {
lot.setEnabled(true);
lnRequired = true;
lot.setError("Lot Number Required");
serial.setEnabled(false);
quantity.setText("");
quantity.setEnabled(true);
findViewById(id).setNextFocusDownId(lotIndex);
} else {
lot.setError(null);
lnRequired = false;
}
有任何想法吗?
我很惊讶这仍然是一个问题。 过去有人问过: 如何以编程方式在 Android 中设置可编辑的真/假 EditText?
也许试试这个,看看会发生什么?
我能够弄清楚。 对字段索引进行硬编码是错误的方法。 我能够通过在 fieldList 数组上使用 for 循环来获得正确的索引,并根据字段的名称将索引分配给变量。 一旦我这样做了,我就可以根据需要 setEnabled(true) 或 setEnabled(false) 没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.