这个是封装好的一个Edtext
public class KeepDecimalEditText extends AppCompatEditText {
public int DECIMAL_DIGITS = 5;//可输入小数的位数
public int defaultDecimal = 5;//显示小数的位数
public boolean isChangeDecimal = false;//是否转换只输入整数
public boolean isInput = false;//是否是输入的
public interface ICallBack {
void onInputEditText(String str);
}
ICallBack icallBack = null;
public void setOnInputEditText(ICallBack iBack) {
icallBack = iBack;
}
public KeepDecimalEditText(Context context) {
super(context, null);
}
public KeepDecimalEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.KeepDecimalEditText, 0, 0);
typedArray.getIndexCount();
typedArray.getIndex(0);
DECIMAL_DIGITS = typedArray.getInteger(R.styleable.KeepDecimalEditText_decimal, 5);
defaultDecimal = typedArray.getInteger(R.styleable.KeepDecimalEditText_decimal, 5);
initOnTouchListener();
initTextChangedListener();
}
public void setDECIMAL_DIGITS() {
this.DECIMAL_DIGITS = defaultDecimal;
isInput = false;
}
public void setDECIMAL_DIGITS(int DECIMAL_DIGITS) {
this.DECIMAL_DIGITS = DECIMAL_DIGITS;
}
public int getDECIMAL_DIGITS() {
return DECIMAL_DIGITS;
}
public void setChangeDecimal(boolean changeDecimal) {
isChangeDecimal = changeDecimal;
}
private void initTextChangedListener() {
addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().contains(".")) {
if ((s.toString()+"a").split("\.").length > 2){//防止输入多个“.”
s = s.toString().substring(0,s.toString().length() -1);
setText(s);
setSelection(s.length());
}
if (s.length() - 1 - s.toString().indexOf(".") == 0 && DECIMAL_DIGITS == 0) {
s = s.toString().subSequence(0, s.toString().indexOf("."));
setText(s);
setSelection(s.length());
return;
}
if (s.length() - 1 - s.toString().indexOf(".") > DECIMAL_DIGITS) {
if (DECIMAL_DIGITS == 0) {
s = s.toString().subSequence(0,
s.toString().indexOf(".") + DECIMAL_DIGITS);
} else {
s = s.toString().subSequence(0,
s.toString().indexOf(".") + DECIMAL_DIGITS + 1);
}
setText(s);
setSelection(s.length());
}
}
if (s.toString().trim().equals(".")) {
s = "0" + s;
setText(s);
setSelection(s.toString().length());
}
if (s.toString().startsWith("0")
&& s.toString().trim().length() > 1) {
if (DECIMAL_DIGITS == 0) {
setText(s.subSequence(1, s.toString().length()));
setSelection(1);
return;
}
// else if (!s.toString().substring(1).equals(".")) {//注释掉,否则无法输入0.*
// setText(s.subSequence(0, 1));
// setSelection(1);
// return;
// }
}
if (icallBack != null && isInput) {
icallBack.onInputEditText(s.toString());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void initOnTouchListener() {
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
isInput = true;
if (isChangeDecimal) {
DECIMAL_DIGITS = 0;
}else{
DECIMAL_DIGITS = defaultDecimal;
}
return false;
}
});
}
}
然后在 Values中创建一个 attrs.xml文件
使用到的一些工具类
public class CalculateUtils {
//保留五位小数
public static String keepTwoDecimal(double data){
return new DecimalFormat("#0.00").format(data);
}
//获取件的数量
public static Double getPieceCount(Double currentCount, List packageConfigsBeen){
int pieceUom = 1;
for (PackageConfigsBean packageConfigsBean : packageConfigsBeen){
if ("EA".equals(packageConfigsBean.getPackageCode())){
pieceUom = packageConfigsBean.getContainerValue();
}
}
return currentCount / pieceUom;
}
//字符串转换成double类型
public static Double stringToDouble(String value){
if (TextUtils.isEmpty(value)){
return 0d;
}
return Double.valueOf(value);
}
public static String controlDecimal(String data){
if (TextUtils.isEmpty(data)){
return "";
}
if (data.contains(".")){
}
return data;
}
}
使用方法 xml布局中
app:decimal=“2” 是保留的小数位数
Activity中的使用方法
etCurrentIncome.setOnInputEditText(new KeepDecimalEditText.ICallBack() {
@Override
public void onInputEditText(String str) {
// inputCurrentIncome = judgeNum(str, currentUom, asnResponseBean.getPackageConfigs());
if (!TextUtils.isEmpty(etWeightOfGoods.getText().toString()) &&
!TextUtils.isEmpty(str)) {
//先转换为件的数量
Double pieceCount = CalculateUtils.stringToDouble(str);
if (Double.valueOf(etWeightOfGoods.getText().toString()) >= 0) {
etGoodsWeight.setText(CalculateUtils.keepTwoDecimal(pieceCount * Double.valueOf(etWeightOfGoods.getText().toString())));
}
}
}
});



