如何通过onKeyUp()找到两个加法结果的差?

时间:2019-04-05 05:16:09

标签: android

我有八个EditText,五个用于收入,三个用于扣除,并且通过调用addTextChangedListener()在所有EditText上设置了TextWatcher。

现在,我将前五个EditText的总和设置为TextView(value),将其他三个EditText的总和设置为TextView(ded_value)。无论哪个EditText都要填充,总和都是在相应的TextView上设置。

这些TextView显示的是唯一的总收入和总扣除额,现在我正在尝试设置应支付的净工资,这将是值ded_value(意味着总收入减去总收入=净应支付的工资)。

并且此差异的结果应显示在另一个Texts上,该值为netsalaryvalue。无论TextView要更改哪个值(值,ded_value),都应在TextView(netsalaryvalue)上设置差异。

enter code here //this is income salry & deduction salary code
textWatcher=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) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        //income code value
        b_salary=et_bs.getText().toString();
        d_allowance=et_da.getText().toString();
        hr_allowance=et_hra.getText().toString();
        t_allowance=et_ta.getText().toString();
        m_allowance=et_ma.getText().toString();

        //deduction code value
        itax=et_itax.getText().toString();
        pf=et_pf.getText().toString();
        tds=et_tds.getText().toString();

        if (!et_bs.getText().toString().isEmpty())
        {
            value.setText("");
            value.setText(b_salary);
        if (!et_da.getText().toString().isEmpty())
        {
            value.setText(String.valueOf(Integer.parseInt(b_salary)+Integer.parseInt(d_allowance)));
            if (!et_hra.getText().toString().isEmpty())
            {
                value.setText(String.valueOf(Integer.parseInt(b_salary)+Integer.parseInt(d_allowance)+Integer.parseInt(hr_allowance)));
                if (!et_ta.getText().toString().isEmpty())
                {
                    value.setText(String.valueOf(Integer.parseInt(b_salary)+Integer.parseInt(d_allowance)+Integer.parseInt(hr_allowance)+Integer.parseInt(t_allowance)));
                    if (!et_ma.getText().toString().isEmpty())
                    {
                        value.setText(String.valueOf(Integer.parseInt(b_salary)+Integer.parseInt(d_allowance)+Integer.parseInt(hr_allowance)+Integer.parseInt(t_allowance)+Integer.parseInt(m_allowance)));
                    }
                }
            }
        }
        }

        if (!et_itax.getText().toString().isEmpty())
        {
            ded_value.setText("");
            ded_value.setText(itax);
            if (!et_pf.getText().toString().isEmpty())
            {
                ded_value.setText(String.valueOf(Integer.parseInt(itax)+Integer.parseInt(pf)));
                if (!et_tds.getText().toString().isEmpty())
                {
                    ded_value.setText(String.valueOf(Integer.parseInt(itax)+Integer.parseInt(pf)+Integer.parseInt(tds)));
                }
            }
        }

    }
};
et_bs.addTextChangedListener(textWatcher);
et_da.addTextChangedListener(textWatcher);
et_hra.addTextChangedListener(textWatcher);
et_ta.addTextChangedListener(textWatcher);
et_ma.addTextChangedListener(textWatcher);
et_itax.addTextChangedListener(textWatcher);
et_pf.addTextChangedListener(textWatcher);
et_tds.addTextChangedListener(textWatcher);

// this is net payable salry code
textWatcher2=new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        netsalaryvalue.setText("");
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {


    }

    @Override
    public void afterTextChanged(Editable s) {

        totalincome=value.getText().toString();
        totaldedu=ded_value.getText().toString();

            if (!value.getText().toString().equals("")||!ded_value.getText().toString().equals(""))
            {
                if (!value.getText().toString().equals("00.00")||!ded_value.getText().toString().equals("00.00"))
                {
                    netsalaryvalue.setText(String.valueOf(Integer.parseInt(totalincome)-Integer.parseInt(totaldedu)));
                }
            }
    }
};

value.addTextChangedListener(textWatcher2);
ded_value.addTextChangedListener(textWatcher2);

强文本,当我只找到总和时,我的代码就可以自由运行,但是当我为netpayablesalary编写代码时,我的应用将崩溃。现在,我正在尝试解决此问题,但仍然无法解决,请帮助我。

0 个答案:

没有答案