为了账号安全,请及时绑定邮箱和手机立即绑定

在多个 EditText 上添加值并在每个 textchange 上获取总计

在多个 EditText 上添加值并在每个 textchange 上获取总计

呼唤远方 2022-05-21 19:49:34
EditText如何在不使用Button但仅使用 TextWatcher 的onTextChanged()方法的情况下对我的倍数进行总和。假设这是我的布局:编辑文本1 = 5编辑文本2 = 5编辑文本3 = 5编辑文本4 = 5总计 = 20等等并得到它的总数?当' 的值发生变化时,总数应该是变化的。EditText我已阅读此答案。但我不能在我的程序上很好地执行它。这是我的布局
查看完整描述

2 回答

?
森林海

TA贡献2011条经验 获得超2个赞

你需要使用一个TextWatcher()


而不是使用 5TextWatcher()你可以只使用一个来管理它TextWatcher()


试试这个


如果您的所有 4 个四个都不为空,则使用以下答案,edittext那么它将计算值的editext总和


public class MainActivity extends AppCompatActivity {



    EditText edtOne, edtTwo, edtThree, edtFour;

    TextView tvResult;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        edtOne = findViewById(R.id.edtOne);

        edtTwo = findViewById(R.id.edtTwo);

        edtThree = findViewById(R.id.edtThree);

        edtFour = findViewById(R.id.edtFour);


        tvResult = findViewById(R.id.tvResult);


        edtOne.addTextChangedListener(textWatcher);

        edtTwo.addTextChangedListener(textWatcher);

        edtThree.addTextChangedListener(textWatcher);

        edtFour.addTextChangedListener(textWatcher);


    }


    TextWatcher textWatcher = new TextWatcher() {

        @Override

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


        }


        @Override

        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            if (!TextUtils.isEmpty(edtOne.getText().toString().trim())

                    || !TextUtils.isEmpty(edtTwo.getText().toString().trim())

                    || !TextUtils.isEmpty(edtThree.getText().toString().trim())

                    || !TextUtils.isEmpty(edtFour.getText().toString().trim())

                    ) {



                int answer = Integer.parseInt(edtOne.getText().toString().trim()) +

                        Integer.parseInt(edtTwo.getText().toString().trim()) +

                        Integer.parseInt(edtThree.getText().toString().trim()) +

                        Integer.parseInt(edtFour.getText().toString().trim());


                Log.e("RESULT", String.valueOf(answer));

                tvResult.setText(String.valueOf(answer));

            }else {

            tvResult.setText("");

        }

        }


        @Override

        public void afterTextChanged(Editable editable) {


        }

    };


}

更新

如果您想计算所有的值editext即使您的编辑文本为空,也请尝试以下操作TextWatcher()


TextWatcher textWatcher = new TextWatcher() {

    @Override

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


    }


    @Override

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


        if (!TextUtils.isEmpty(edtOne.getText().toString().trim())

                || !TextUtils.isEmpty(edtTwo.getText().toString().trim())

                || !TextUtils.isEmpty(edtThree.getText().toString().trim())

                || !TextUtils.isEmpty(edtFour.getText().toString().trim())

                ) {



            int firtValue = TextUtils.isEmpty(edtOne.getText().toString().trim()) ? 0 : Integer.parseInt(edtOne.getText().toString().trim());

            int secondValue = TextUtils.isEmpty(edtTwo.getText().toString().trim()) ? 0 : Integer.parseInt(edtTwo.getText().toString().trim());

            int thirdValue = TextUtils.isEmpty(edtThree.getText().toString().trim()) ? 0 : Integer.parseInt(edtThree.getText().toString().trim());

            int forthValue = TextUtils.isEmpty(edtFour.getText().toString().trim()) ? 0 : Integer.parseInt(edtFour.getText().toString().trim());


            int answer = firtValue + secondValue + thirdValue + forthValue;


            Log.e("RESULT", String.valueOf(answer));

            tvResult.setText(String.valueOf(answer));

        }else {

            tvResult.setText("");

        }

    }


    @Override

    public void afterTextChanged(Editable editable) {


    }

};


查看完整回答
反对 回复 2022-05-21
?
www说

TA贡献1775条经验 获得超8个赞

我将告诉你如何去做,但我不会将解决方案作为总代码发布。


首先,定义整数,即编辑文本计数的数量。如果有 4 个编辑文本,用 like 定义 4 个整数int first = 0, second = 0, third = 0, fourth = 0;


然后,将单独的文本观察器添加到您的编辑文本中,并在您的afterTextChanged方法上,通过使用从字符串中获取整数值,Integer.parseInt(Editable s.getText.toString())并确保输入是可转换的,否则您将获得一个NumberFormatException. 然后,将此值分配给相关的编辑文本,例如在您第一次使用编辑文本时first = Integer.parse(s.getText().toString())。


创建一个使用这些变量在文本视图上显示的函数。例如:


private void showResult() {

    textView.setText(String.valueOf(first + second + third + fourth));

}

然后,在每个 afterTextChanged 方法调用此函数以显示总金额。


查看完整回答
反对 回复 2022-05-21
  • 2 回答
  • 0 关注
  • 221 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号