package com.example.administrator.zhapp.utils;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Toast;

import com.orhanobut.logger.Logger;

/**
 * EditText文本变化监听器
 */
public class EditTextListener implements TextWatcher {

    private String msg;

    public void setNum(String num) {
        this.num = num;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    private String num;
    private boolean flag;
    private Context context;

    public EditTextListener(Context context) {
        this.context = context;
    }

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

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        Logger.v("输入数据：" + charSequence + "====" + num);
        double inputnum = 0;
        try {
            inputnum = Double.parseDouble(num);

            //数据在0-15位数之间
            if (charSequence.length() > 0 && charSequence.length() < 15) {
                if (Double.parseDouble(charSequence.toString()) > inputnum) {
                    flag = true;
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                }

//                if (Double.parseDouble(charSequence.toString()) == 0) {
//                    Toast.makeText(context, "不能输入0", Toast.LENGTH_SHORT).show();
//                    flag = true;
//                }

            } else {
                //清空edittext的时候
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (flag) {
            editable.clear();
            flag = false;
        }
    }
}
