如何同时修改多个EditText的输入,就像我只修改一个一样?

huangapple go评论160阅读模式
英文:

How can I modify the input of multiple EditTexts at the same time, as I only modify one?

问题

我有多个 EditText,我希望在修改一个 EditText 的输入时,同时更改所有 EditText 的输入(它们都以十进制数作为输入)。

我将这些 EditText 存储在名为 'editTexts' 的数组中。

以下是我尝试过的代码:

  1. // 为每个元素设置监听器
  2. for (int i = 0; i < editTexts.length; i++) {
  3. final int finalI = i;
  4. editTexts[i].addTextChangedListener(new TextWatcher() {
  5. @Override
  6. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  7. }
  8. @Override
  9. public void onTextChanged(CharSequence s, int start, int before, int count) {
  10. // 如果当前正在编辑的 EditText 为空,则将其余所有 EditText 的输入设置为 '0.0'
  11. if (editTexts[finalI].getText().toString().trim().length() == 0) {
  12. for (EditText e : editTexts) {
  13. if (e == editTexts[finalI])
  14. continue;
  15. e.setText("0.0");
  16. }
  17. } else {
  18. float no = Float.parseFloat(s.toString() + "");
  19. // 将所有其他 EditText 的输入设置为输入的小数乘以 2
  20. for (EditText e : editTexts) {
  21. if (e == editTexts[finalI])
  22. continue;
  23. e.setText(no * 2 + "");
  24. }
  25. }
  26. }
  27. @Override
  28. public void afterTextChanged(Editable s) {
  29. }
  30. });
  31. }

在这种情况下,乘法系数只是一个示例,它不总是为 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

  1. //Set the listener for each element
  2. for (int i=0; i&lt;editTexts.length; i++) {
  3. final int finalI = i;
  4. editTexts[i].addTextChangedListener(new TextWatcher() {
  5. @Override
  6. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  7. }
  8. @Override
  9. public void onTextChanged(CharSequence s, int start, int before, int count) {
  10. //if the editText which is currently edited is empty, set the input for all the rest to be &#39;0.0&#39;
  11. if (editTexts[finalI].getText().toString().trim().length() == 0) {
  12. for(EditText e : editTexts) {
  13. if (e == editTexts[finalI])
  14. continue;
  15. e.setText(&quot;0.0&quot;);
  16. }
  17. } else {
  18. float no = Float.parseFloat(s.toString() + &quot;&quot;);
  19. //Set the input of all the other editTexts to be the decimal number entered, multiplied by 2
  20. for(EditText e : editTexts){
  21. if(e == editTexts[finalI])
  22. continue;
  23. e.setText(no*2+&quot;&quot;);
  24. }
  25. }
  26. }
  27. @Override
  28. public void afterTextChanged(Editable s) {
  29. }
  30. })
  31. }

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

  1. // et_x1、et_x2和et_x3是EditText的ID
  2. // 将所有EditText的inputType设置为numberDecimal
  3. EditText editText1 = findViewById(R.id.et_x1);
  4. final EditText editText2 = findViewById(R.id.et_x2);
  5. final EditText editText3 = findViewById(R.id.et_x3);
  6. editText1.addTextChangedListener(new TextWatcher() {
  7. @Override
  8. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  9. }
  10. @Override
  11. public void onTextChanged(CharSequence s, int start, int before, int count) {
  12. String value = s.toString();
  13. double x;
  14. if (!value.equals("")) {
  15. x = Double.parseDouble(value);
  16. } else {
  17. x = 0.0;
  18. }
  19. editText2.setText(Editable.Factory.getInstance().newEditable((String.valueOf(Math.pow(x, 2)))));
  20. editText3.setText(
  21. Editable.Factory.getInstance().newEditable((String.valueOf(Math.pow(x, 3)))));
  22. }
  23. @Override
  24. public void afterTextChanged(Editable s) {
  25. }
  26. });

希望对你有所帮助!

英文:

Try it like this:

  1. // et_x1, et_x2 and et_x3 are ids of EditTexts
  2. //set inputType for all EditTexts as numberDecimal
  3. EditText editText1 = findViewById(R.id.et_x1);
  4. final EditText editText2 = findViewById(R.id.et_x2);
  5. final EditText editText3 = findViewById(R.id.et_x3);
  6. editText1.addTextChangedListener(new TextWatcher() {
  7. @Override
  8. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  9. }
  10. @Override
  11. public void onTextChanged(CharSequence s, int start, int before, int count) {
  12. String value = s.toString();
  13. double x;
  14. if (!value.equals(&quot;&quot;)) {
  15. x = Double.parseDouble(value);
  16. } else {
  17. x = 0.0;
  18. }
  19. editText2.setText(Editable.Factory.getInstance().newEditable((String.valueOf(Math.pow(x, 2)))));
  20. editText3.setText(
  21. Editable.Factory.getInstance().newEditable((String.valueOf(Math.pow(x, 3)))));
  22. }
  23. @Override
  24. public void afterTextChanged(Editable s) {
  25. }
  26. });

Hope it helps you!

huangapple
  • 本文由 发表于 2020年5月30日 21:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/62103423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定