英文:
How can I modify the input of multiple EditTexts at the same time, as I only modify one?
问题
我有多个 EditText,我希望在修改一个 EditText 的输入时,同时更改所有 EditText 的输入(它们都以十进制数作为输入)。
我将这些 EditText 存储在名为 'editTexts' 的数组中。
以下是我尝试过的代码:
// 为每个元素设置监听器
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) {
// 如果当前正在编辑的 EditText 为空,则将其余所有 EditText 的输入设置为 '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() + "");
// 将所有其他 EditText 的输入设置为输入的小数乘以 2
for (EditText e : editTexts) {
if (e == editTexts[finalI])
continue;
e.setText(no * 2 + "");
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
在这种情况下,乘法系数只是一个示例,它不总是为 2。我只是用它进行测试。
出现某种原因,当我更改输入值时,应用程序会冻结。
需要帮助吗?谢谢!
英文:
I have multiple EditTexts and I want to change the input of all of them at the same time, as I modify only one.(all of them take decimal numbers as input)
I stored the EditTexts in an array named 'editTexts'.
Here's what I tried
//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) {
}
})
}
In this case the multiplication coefficient is just an example, it's not always gonna be 2. I used it just to test it out.
For some reason, when I change the input value, the app freezes.
Any help? Thanks!
答案1
得分: 2
使用LiveData
来存储您的用户输入值。
一旦它的值发生变化,您可以将值设置给每个EditText
。我认为这是一种简单的实现方式。
英文:
Use LiveData
to store your user input values.
Once it's value changes you can set value to each EditText
. I think it is an easy way to implement.
答案2
得分: 2
// et_x1、et_x2和et_x3是EditText的ID
// 将所有EditText的inputType设置为numberDecimal
EditText editText1 = findViewById(R.id.et_x1);
final EditText editText2 = findViewById(R.id.et_x2);
final EditText editText3 = findViewById(R.id.et_x3);
editText1.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) {
String value = s.toString();
double x;
if (!value.equals("")) {
x = Double.parseDouble(value);
} else {
x = 0.0;
}
editText2.setText(Editable.Factory.getInstance().newEditable((String.valueOf(Math.pow(x, 2)))));
editText3.setText(
Editable.Factory.getInstance().newEditable((String.valueOf(Math.pow(x, 3)))));
}
@Override
public void afterTextChanged(Editable s) {
}
});
希望对你有所帮助!
英文:
Try it like this:
// et_x1, et_x2 and et_x3 are ids of EditTexts
//set inputType for all EditTexts as numberDecimal
EditText editText1 = findViewById(R.id.et_x1);
final EditText editText2 = findViewById(R.id.et_x2);
final EditText editText3 = findViewById(R.id.et_x3);
editText1.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) {
String value = s.toString();
double x;
if (!value.equals("")) {
x = Double.parseDouble(value);
} else {
x = 0.0;
}
editText2.setText(Editable.Factory.getInstance().newEditable((String.valueOf(Math.pow(x, 2)))));
editText3.setText(
Editable.Factory.getInstance().newEditable((String.valueOf(Math.pow(x, 3)))));
}
@Override
public void afterTextChanged(Editable s) {
}
});
Hope it helps you!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论