簡體   English   中英

Android EditText setEnabled(false) 不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM