简体   繁体   English

Android EditText setEnabled(false) 不起作用

[英]Android EditText setEnabled(false) isn't working

I have a couple of dynamically created EditText fields.我有几个动态创建的 EditText 字段。 I am trying to disable the fields if the value in another field doesn't match certain criteria.如果另一个字段中的值与某些条件不匹配,我将尝试禁用这些字段。 The issue is that if other criteria is met, the field is disabled properly by setting setEnabled(false);问题是,如果满足其他条件,则通过设置setEnabled(false);正确禁用该字段setEnabled(false); . .

Essentially in a part number field, we check if the part number requires a serial number or a lot number.基本上在零件编号字段中,我们检查零件编号是否需要序列号或批号。 If the part requires a serial number, the serial number field displays an error, the lot number field is disabled, and the quantity field is set to "1" and disabled If a lot number is required, the lot number field shows an error and the serial number field is disabled.如果零件需要序列号,序列号字段显示错误,批号字段禁用,数量字段设置为“1”并禁用 如果需要批号,批号字段显示错误和序列号字段被禁用。 This works as intended.这按预期工作。

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;
                                }

Any ideas?有任何想法吗?

I'm surprised this is still an issue.我很惊讶这仍然是一个问题。 It's been asked before way back in the past: How to set editable true/false EditText in Android programmatically?过去有人问过: 如何以编程方式在 Android 中设置可编辑的真/假 EditText?

Maybe try this and see what comes of it?也许试试这个,看看会发生什么?

I was able to figure it out.我能够弄清楚。 Hard coding the field indexes was the wrong approach.对字段索引进行硬编码是错误的方法。 I was able to get the proper indexes by using a for loop on a fieldList array, and assigning the indexes to the variables based on the name of the field.我能够通过在 fieldList 数组上使用 for 循环来获得正确的索引,并根据字段的名称将索引分配给变量。 Once I did that, I was able to setEnabled(true) or setEnabled(false) as needed without issue.一旦我这样做了,我就可以根据需要 setEnabled(true) 或 setEnabled(false) 没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM