概述
以下代码在项目里,项目地址:https://gitee.com/urasaki/RxJava2AndRetrofit2
1、修改页面的layout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.EditTextInputLimitsActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_75"
android:background="@color/color_FF198CFF"
android:minHeight="?attr/actionBarSize"
android:orientation="vertical"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_75"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_25" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50">
<FrameLayout
android:id="@+id/layout_back"
android:layout_width="@dimen/dp_36"
android:layout_height="match_parent"
android:gravity="start"
tools:ignore="UselessParent">
<ImageView
android:id="@+id/imv_back"
android:layout_width="@dimen/dp_6"
android:layout_height="@dimen/dp_11"
android:layout_gravity="center"
android:src="@mipmap/icon_arrowhead_left"
tools:ignore="ContentDescription" />
</FrameLayout>
<TextView
android:id="@+id/tev_title"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:gravity="center"
android:text="@string/edit_text_input_limits"
android:textColor="@color/color_FFFFFFFF"
android:textSize="@dimen/sp_20" />
</FrameLayout>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:paddingStart="@dimen/dp_10"
android:paddingEnd="@dimen/dp_10"
android:text="Android EditText限制只能输入整数和小数,且数字总长度小于等于11位(可自定义),整数5位(可自定义),小数5位(可自定义)"
android:textColor="@color/black"
android:textSize="@dimen/sp_16" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50" />
<TextView
android:id="@+id/tev_show_input"
android:layout_width="@dimen/dp_280"
android:layout_height="@dimen/dp_40"
android:layout_gravity="center_horizontal"
android:background="@drawable/corners_14_color_white_stroke_1_color_80000000"
android:gravity="center"
android:textColor="@color/colorBlack333"
android:textSize="@dimen/sp_16"
tools:ignore="Autofill,LabelFor,TextFields" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50" />
<TextView
android:id="@+id/tev_start_input"
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_40"
android:layout_gravity="center_horizontal"
android:background="@drawable/corners_14_color_white_stroke_1_color_80000000"
android:gravity="center"
android:paddingStart="@dimen/dp_10"
android:paddingEnd="@dimen/dp_10"
android:text="start input"
android:textColor="@color/black"
android:textSize="@dimen/sp_16" />
</LinearLayout>
2、初始化EditText,并设置输入数字总长度小于等于11位(可自定义),整数5位(可自定义),小数5位(可自定义)。
private Toolbar toolbar;
private FrameLayout layoutBack;
private ImageView imvBack;
private TextView tevTitle;
private TextView tevShowInput;
private TextView tevStartInput;
toolbar = (Toolbar) findViewById(R.id.toolbar);
layoutBack = (FrameLayout) findViewById(R.id.layout_back);
imvBack = (ImageView) findViewById(R.id.imv_back);
tevTitle = (TextView) findViewById(R.id.tev_title);
tevShowInput = (TextView) findViewById(R.id.tev_show_input);
tevStartInput = (TextView) findViewById(R.id.tev_start_input);
setToolbar(false, R.color.color_FF198CFF);
imvBack.setColorFilter(ContextCompat.getColor(rxAppCompatActivity, R.color.white));
tevStartInput.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_layout_edit_text_input_limits, 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 -> {
MineInputMethodManager.hideInputMethod(this);
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.tevShowInput.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.tevShowInput.setText(edtInput.getText().toString());
alertDialog.dismiss();
}
} else {
Toast.makeText(this, "整数长度不能大于" + beforeDecimalNum + "位", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(this, "请输入整数或小数", Toast.LENGTH_SHORT).show();
}
});
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
edtInput.setFocusable(true);
edtInput.setFocusableInTouchMode(true);
edtInput.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
MineInputMethodManager.showInputMethod(EditTextInputLimitsActivity.this, edtInput);
} else {
}
}
});
edtInput.requestFocus();
}
4、添加自定义Dialog的layout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/dp_300"
android:layout_height="@dimen/dp_240"
android:background="@drawable/corners_14_color_white"
android:orientation="vertical">
<LinearLayout
android:layout_width="@dimen/dp_300"
android:layout_height="@dimen/dp_240"
android:orientation="vertical"
tools:ignore="UselessParent">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_20" />
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:paddingStart="@dimen/dp_10"
android:paddingEnd="@dimen/dp_10"
android:text="Decimal"
android:textColor="@color/blue"
android:textSize="@dimen/sp_16" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_20" />
<EditText
android:id="@+id/edt_input"
android:layout_width="@dimen/dp_240"
android:layout_height="@dimen/dp_40"
android:layout_gravity="center_horizontal"
android:background="@drawable/corners_14_color_white_stroke_1_color_80000000"
android:gravity="center_vertical"
android:paddingStart="@dimen/dp_15"
android:paddingEnd="@dimen/dp_15"
android:textColor="@color/colorBlack333"
android:textSize="@dimen/sp_16" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_20" />
<FrameLayout
android:layout_width="@dimen/dp_200"
android:layout_height="@dimen/dp_40"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/tev_cancel"
android:layout_width="@dimen/dp_80"
android:layout_height="@dimen/dp_40"
android:layout_gravity="start"
android:background="@drawable/corners_14_color_white_stroke_1_color_80000000"
android:gravity="center"
android:paddingStart="@dimen/dp_10"
android:paddingEnd="@dimen/dp_10"
android:text="cancel"
android:textColor="@color/color_80000000"
android:textSize="@dimen/sp_16" />
<TextView
android:id="@+id/tev_confirm"
android:layout_width="@dimen/dp_80"
android:layout_height="@dimen/dp_40"
android:layout_gravity="end"
android:background="@drawable/corners_14_color_white_stroke_1_color_blue"
android:gravity="center"
android:paddingStart="@dimen/dp_10"
android:paddingEnd="@dimen/dp_10"
android:text="confirm"
android:textColor="@color/blue"
android:textSize="@dimen/sp_16" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
5、在res文件夹的drawable文件夹新建几个xml文件,并把一下的xml代码复制进去。
corners_14_color_white的drawable文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/dp_14" />
<solid android:color="@color/white" />
</shape>
corners_14_color_white_stroke_1_color_80000000的drawable文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/dp_14" />
<solid android:color="@color/white" />
<stroke
android:width="@dimen/dp_1"
android:color="@color/color_80000000" />
</shape>
corners_14_color_white_stroke_1_color_blue的drawable文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="@dimen/dp_14" />
<solid android:color="@color/white" />
<stroke
android:width="@dimen/dp_1"
android:color="@color/blue" />
</shape>
6、添加DecimalInputFilter类。
package com.phone.common_library.common;
import android.text.InputFilter;
import android.text.Spanned;
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;
}
}
7、添加DecimalTextWatcher类。
package com.phone.common_library.common;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
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<String> onCommonSingleParamCallback;
private int index;
public DecimalTextWatcher(EditText editText, int afterDecimalNum) {
this.editText = editText;
this.afterDecimalNum = afterDecimalNum;
}
// public DecimalTextWatcher(EditText editText, int afterDecimalNum, OnCommonSingleParamCallback<String> 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("");
// }
}
}
8、在res文件夹的values文件夹的values.xml文件下加入dialog style。
<style name="dialog_decimal_style" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<!--设置横屏最少宽度-->
<item name="android:windowMinWidthMajor">220mm</item>
</style>
如对此有疑问,请联系qq1164688204。
推荐Android开源项目
项目功能介绍:RxJava2和Retrofit2项目,添加自动管理token功能,添加RxJava2生命周期管理,使用App架构设计是MVP模式和MVVM模式,同时使用组件化,部分代码使用Kotlin,此项目持续维护中。
项目地址:https://gitee.com/urasaki/RxJava2AndRetrofit2
最后
以上就是贪玩荔枝为你收集整理的Android EditText限制只能输入整数和小数,且数字总长度小于等于11位(可自定义),整数5位(可自定义),小数5位(可自定义)的全部内容,希望文章能够帮你解决Android EditText限制只能输入整数和小数,且数字总长度小于等于11位(可自定义),整数5位(可自定义),小数5位(可自定义)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复