Android 实时监听文本框 运算

 项目中遇到的 记录下以防忘记   实时监听文本框的值并计算

  private  double height=0;
  private  double weight=0;
  private  double weightcur=0;


 mViewBinding.edFollowupHeight.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) {
      }
      @Override
      public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(s)) {
          height = Double.parseDouble(s.toString());
          if (height > 0) {
            try {
              if (!TextUtils.isEmpty(mViewBinding.edFollowupWeight.getText().toString())) {
                weight=Double.parseDouble(mViewBinding.edFollowupWeight.getText().toString());
                mViewBinding.edFollowupBMI.setText(div(weight, mul(height / 100, height / 100), 2) + "");
              }
              if (!TextUtils.isEmpty(mViewBinding.edTargetWeight.getText().toString())) {
                weightcur=Double.parseDouble(mViewBinding.edTargetWeight.getText().toString());
                mViewBinding.edTargetBMI.setText(div(weightcur, mul(height / 100, height / 100), 2) + "");
              }
            } catch (IllegalAccessException e) {
              e.printStackTrace();
            }
          }
        }
      }
    });

 

底下这个方法取自   原文链接:https://blog.csdn.net/qq_36255612/article/details/78203878

public class Arith {
    /**
     * 提供精确加法计算的add方法
     *
     * @param value1 被加数
     * @param value2 加数
     * @return 两个参数的和
     */
    public static double add(double value1, double value2) {
        BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
        BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
        return b1.add(b2).doubleValue();
    }
 
    /**
     * 提供精确减法运算的sub方法
     *
     * @param value1 被减数
     * @param value2 减数
     * @return 两个参数的差
     */
    public static double sub(double value1, double value2) {
        BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
        BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
        return b1.subtract(b2).doubleValue();
    }
 
    /**
     * 提供精确乘法运算的mul方法
     *
     * @param value1 被乘数
     * @param value2 乘数
     * @return 两个参数的积
     */
    public static double mul(double value1, double value2) {
        BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
        BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
        return b1.multiply(b2).doubleValue();
    }
 
    /**
     * 提供精确的除法运算方法div
     *
     * @param value1 被除数
     * @param value2 除数
     * @param scale  精确范围
     * @return 两个参数的商
     * @throws IllegalAccessException
     */
    public static double div(double value1, double value2, int scale) throws IllegalAccessException {
        //如果精确范围小于0,抛出异常信息
        if (scale < 0) {
            throw new IllegalAccessException("精确度不能小于0");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(value1));
        BigDecimal b2 = new BigDecimal(Double.toString(value2));
        //默认保留两位会有错误,这里设置保留小数点后4位
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}

 

上一篇:PostgreSQL之INSERT,DELETE,UPDATE


下一篇:SqlServer在关联的情况下的增删改查