由于我只修改一个EditText,如何同时修改多个EditText的输入?

问题描述 投票:0回答:1

我有多个EditText,并且我想同时更改所有它们的输入,因为我只修改了一个。(它们都以十进制数字作为输入)

我将EditTexts存储在名为'editTexts'的数组中。

这是我尝试过的

//Set the listener for each element
for (int i=0; i<editTexts.length; i++) {
        final int finalI = i;
        editTexts[i].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//if the editText which is currently edited is empty, set the input for all the rest to be '0.0'
                if (editTexts[finalI].getText().toString().trim().length() == 0) {
                    for(EditText e : editTexts) {
                        if (e == editTexts[finalI])
                            continue;
                        e.setText("0.0");
                    }
                } else {
                    float no = Float.parseFloat(s.toString() + "");
//Set the input of all the other editTexts to be the decimal number entered, multiplied by 2
                    for(EditText e : editTexts){
                        if(e == editTexts[finalI])
                            continue;
                        e.setText(no*2+"");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        })
    }

在这种情况下,乘法系数只是一个例子,并不总是2。我只是用它来测试它。

由于某些原因,当我更改输入值时,应用程序冻结。

有帮助吗?谢谢!

java android android-edittext multiple
1个回答
2
投票

使用LiveData存储您的用户输入值。值更改后,您可以为每个EditText设置值。我认为这是一种简单的实现方式。

© www.soinside.com 2019 - 2024. All rights reserved.