1、修改页面的layout。
2、初始化EditText,并设置输入数字总长度小于等于11位(可自定义),整数5位(可自定义),小数5位(可自定义)。
EditText edtInput = (EditText) findViewById(R.id.edt_input);
TextView tevConfirm = (TextView) findViewById(R.id.tev_confirm);
tevInput.setOnClickListener(v -> {
showDecimalAlertDialog();
});
3、添加showDecimalAlertDialog方法,注意:如果输入特殊小数或整数,如:.或.15或10.或00035或输入21153.67589,然后删除中间小数点,整数就是10位了(整数不能大于5位),则要在点击Dialog确认按钮之前进行提示,这样就不会填入不符合规范的整数或小数了。
private void showDecimalAlertDialog() {
View view = LayoutInflater.from(this).inflate(R.layout.dialog_decimal_layout, null, false);
EditText edtInput = (EditText) view.findViewById(R.id.edt_input);
TextView tevCancel = (TextView) view.findViewById(R.id.tev_cancel);
TextView tevConfirm = (TextView) view.findViewById(R.id.tev_confirm);
//小数点前边几位(修改这里可以自定义)
int beforeDecimalNum = 5;
//小数点后边几位(修改这里可以自定义)
int afterDecimalNum = 5;
//最大长度是多少位(修改这里可以自定义)
int maxLength = 11;
//输入的类型可以是整数或小数
edtInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
DecimalInputFilter decimalInputFilter = new DecimalInputFilter(beforeDecimalNum, afterDecimalNum);
//输入总长度多少位,小数几位(修改这里可以自定义)
InputFilter[] inputFilter = {new InputFilter.LengthFilter(maxLength), decimalInputFilter};
edtInput.setFilters(inputFilter);
edtInput.addTextChangedListener(new DecimalTextWatcher(edtInput, afterDecimalNum));
@SuppressLint("RestrictedApi")
AlertDialog alertDialog = new AlertDialog.Builder(this, R.style.dialog_decimal_style)
.setView(view, 0, 0, 0, 0)
.show();
// int widthPx = ScreenManager.getScreenWidth(this);
// int widthDp = ScreenManager.pxToDp(this, widthPx);
// int heightPx = ScreenManager.getScreenHeight(this);
// int heightDp = ScreenManager.pxToDp(this, heightPx);
// LogManager.i(TAG, "widthDp*****" + widthDp);
// LogManager.i(TAG, "heightDp*****" + heightDp);
tevCancel.setOnClickListener(v -> {
alertDialog.dismiss();
});
tevConfirm.setOnClickListener(v -> {
String afterData = edtInput.getText().toString();
if (!"".equals(afterData)) {
if (afterData.contains(".")) {
String[] afterDataArr = afterData.split("\\.");
if ("".equals(afterDataArr[0])) {
Toast.makeText(this, "请输入正常整数或小数", Toast.LENGTH_SHORT).show();
} else if (afterDataArr.length == 1) {//当afterData是这种类型的小数时(0. 100.)
Toast.makeText(this, "请输入正常整数或小数", Toast.LENGTH_SHORT).show();
} else {
this.edtInput.setText(edtInput.getText().toString());
alertDialog.dismiss();
}
} else {
if (afterData.length() <= beforeDecimalNum) {
String[] afterDataArr = afterData.split("");
if (afterDataArr.length > 1 && "0".equals(afterDataArr[1])) {
Toast.makeText(this, "请输入正常整数或小数", Toast.LENGTH_SHORT).show();
} else {
this.edtInput.setText(edtInput.getText().toString());
alertDialog.dismiss();
}
} else {
Toast.makeText(this, "整数长度不能大于" + beforeDecimalNum + "位", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(this, "请输入整数或小数", Toast.LENGTH_SHORT).show();
}
});
}
3、添加DecimalInputFilter类。
package com.phone.main_module.edit_text_attribute;
import android.text.InputFilter;
import android.text.Spanned;
/**
* 小数最多xx位(这是第一步处理,第二步见DecimalTextWatcher)
*/
public class DecimalInputFilter implements InputFilter {
private static final String TAG = DecimalInputFilter.class.getSimpleName();
//小数点前边几位
private int beforeDecimalNum;
//小数点后边几位
private int afterDecimalNum;
public DecimalInputFilter(int beforeDecimalNum, int afterDecimalNum) {
this.beforeDecimalNum = beforeDecimalNum;
this.afterDecimalNum = afterDecimalNum;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String beforeData = dest.toString();
String inputData = source.toString();
if (source.length() == 0 && dend > dstart) {
//删除操作
//使用系统的
return null;
} else {
//输入操作
if (beforeData.contains(".")) {
String[] beforeDataArr = beforeData.split("\\.");
if (beforeDataArr.length > 1 && beforeDataArr[0].length() >= beforeDecimalNum && beforeDataArr[1].length() >= afterDecimalNum) {
//返回自定义的
return "";
} else if (beforeDataArr.length >= 1 && beforeDataArr[0].length() >= beforeDecimalNum && dstart <= beforeDecimalNum) {
//返回自定义的
return "";
} else if (beforeDataArr.length > 1 && beforeDataArr[1].length() >= afterDecimalNum && dend >= beforeDecimalNum) {
//返回自定义的
return "";
}
} else {
if (beforeData.length() >= beforeDecimalNum && !inputData.equals(".")) {
//返回自定义的
return "";
}
}
}
//使用系统的
return null;
}
}
4、添加DecimalTextWatcher类。
package com.phone.main_module.edit_text_attribute;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import com.phone.common_library.callback.OnCommonSingleParamCallback;
/**
* 小数最多xx位(这是第二步处理,第一步见DecimalInputFilter)
*/
public class DecimalTextWatcher implements TextWatcher {
private static final String TAG = DecimalTextWatcher.class.getSimpleName();
private String beforeData;
private String[] beforeDataArr;
private EditText editText;
//小数点后边几位
private int afterDecimalNum;
// private OnCommonSingleParamCallback onCommonSingleParamCallback;
private int index;
public DecimalTextWatcher(EditText editText, int afterDecimalNum) {
this.editText = editText;
this.afterDecimalNum = afterDecimalNum;
}
// public DecimalTextWatcher(EditText editText, int afterDecimalNum, OnCommonSingleParamCallback onCommonSingleParamCallback) {
// this.editText = editText;
// this.afterDecimalNum = afterDecimalNum;
// this.onCommonSingleParamCallback = onCommonSingleParamCallback;
// }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
beforeData = s.toString();
beforeDataArr = null;
if (!TextUtils.isEmpty(beforeData) && beforeData.contains(".")) {
beforeDataArr = beforeData.split("\\.");
}
index = editText.getSelectionStart();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String afterData = s.toString();
editText.removeTextChangedListener(this);
if (afterData.contains(".") && afterData.length() > 1) {
if (beforeDataArr != null && beforeDataArr.length > 1) {
for (int i = 0; i < beforeDataArr.length; i++) {
if (afterData.contains(beforeDataArr[i])) {
String[] afterDataArr = afterData.split("\\.");
if (afterDataArr != null && afterDataArr.length > 1 && afterDataArr[1].length() > afterDecimalNum) {
if (index >= afterDataArr[0].length()) {
s.delete(index, index + 1);
}
break;
}
}
}
}
}
editText.addTextChangedListener(this);
// afterData = s.toString();
// if (!TextUtils.isEmpty(afterData)) {
// onCommonSingleParamCallback.onSuccess(afterData);
// } else {
// onCommonSingleParamCallback.onError("");
// }
}
}
5、在res文件夹的values文件夹的values.xml文件下加入dialog style。
如对此有疑问,请联系qq1164688204。
推荐Android开源项目
项目功能介绍:RxJava2和Retrofit2项目,添加自动管理token功能,添加RxJava2生命周期管理,使用App架构设计是MVP模式和MVVM模式,同时使用组件化,部分代码使用Kotlin,此项目持续维护中。
项目地址:https://gitee.com/urasaki/RxJava2AndRetrofit2