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

时间:2019-04-03 11:31:48

标签: android

[在此处输入图片描述] [1]我有八个EditText,五个用于Income,三个用于扣除,并且我通过调用addTextChangedListener()在所有EditText上设置了TextWatcher

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

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

此差异的结果应在另一个为netsalaryvalue的TextView上显示。无论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);

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

3 个答案:

答案 0 :(得分:0)

这样做

textWatcher=new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        value.setText("");


    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        basic=et_bs.getText().toString();
        allowance=et_da.getText().toString();
        if(!basic.isEmpty() && !allowance.isEmpty())
             value.setText(""+(Integer.parseInt(basic)+Integer.parseInt(allowance)));

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

您还可以在onAfterTextChanged中添加这三行

@Override
public void afterTextChanged(Editable s) {
   basic=et_bs.getText().toString();
   allowance=et_da.getText().toString();
   if(!basic.isEmpty() && !allowance.isEmpty())
       value.setText(""+(Integer.parseInt(basic)+Integer.parseInt(allowance)));
}

答案 1 :(得分:0)

您可以尝试

 @Override
  public void onTextChanged(CharSequence s, int start, 
   int before, int count) {
     basic=et_bs.getText().toString();
     allowance=et_da.getText().toString();
     ////Check for null/empty/Not-Integer value
     int first = 0;
     int second = 0;
     try {
      first = Integer.parseInt(basic);
      second = Integer.parseInt(allowance);
      }
      catch(Exception e ) {
     }
       value.setText(""+String.valueOf(first+second)));
    }

答案 2 :(得分:0)

强文本根据我的问题,确切答案是:-

enter code here   public void afterTextChanged(Editable s) {

            basic=et_bs.getText().toString();
            allowance=et_da.getText().toString();
            value.setText(basic);
            if (!et_da.getText().toString().isEmpty())
            {
                value.setText(""+(Integer.parseInt(basic)+Integer.parseInt(allowance)));
            }

        }

强文本,现在可以正常运行了。