简体   繁体   中英

Android EditText setEnabled(false) isn't working

I have a couple of dynamically created EditText fields. 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); .

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. 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?

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. Once I did that, I was able to setEnabled(true) or setEnabled(false) as needed without issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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