在开发过程中,往往会由于需求原因需要我们去定制一些符合要求的自定义控件,方便进行功能开发,其中小数位的输入限制便是最常见的功能之一。
效果图:
主要代码如下:
/**
* @Description 自定义小数输入框
* @Author 一花一世界
*/
public class DrEditText extends EditText {
private int decimalPlaces = 4;//默认最多输入两位小数
public DrEditText(Context context) {
super(context);
initView();
}
public DrEditText(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public DrEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
addTextChangedListener(new TextWatcher() {
boolean deleteLastChar;// 是否需要删除末尾
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().contains(".")) {
// 如果点后面有超过三位数值,则删掉最后一位
int length = s.length() - s.toString().lastIndexOf(".");
// 说明后面有三位数值
deleteLastChar = length >= decimalPlaces;
}
}
@Override
public void afterTextChanged(Editable s) {
if (s == null) {
return;
}
if (deleteLastChar) {
// 设置新的截取的字符串
setText(s.toString().substring(0, s.toString().length() - 1));
// 光标强制到末尾
setSelection(getText().length());
}
// 以小数点开头,前面自动加上 "0"
if (s.toString().startsWith(".")) {
setText("0" + s);
setSelection(getText().length());
}
}
});
setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
EditText mEditText = (EditText) v;
// 以小数点结尾,去掉小数点
if (!hasFocus && mEditText.getText() != null && mEditText.getText().toString().endsWith(".")) {
setText(mEditText.getText().subSequence(0, mEditText.getText().length() - 1));
setSelection(getText().length());
}
}
});
}
/**
* @Description 设置输入位数
*/
public void setDecimalPlaces(int decimalPlaces) {
this.decimalPlaces = decimalPlaces + 2;
}
/**
* @Description 获取输入位数
*/
public int getDecimalPlaces() {
return decimalPlaces - 2;
}
}
布局中使用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/theme_bg"
android:orientation="vertical">
<com.wiggins.decimalrestrictions.widget.TitleView
android:id="@+id/titleView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.wiggins.decimalrestrictions.widget.DrEditText
android:id="@+id/edt_input"
android:layout_width="match_parent"
android:layout_height="@dimen/item_normal"
android:layout_margin="@dimen/margin_normal"
android:background="@color/white"
android:gravity="center"
android:hint="@string/please_enter_decimal"
android:inputType="numberDecimal"
android:maxLength="10"
android:textColor="@color/blue"
android:textColorHint="@color/gray"
android:textCursorDrawable="@null"
android:textSize="@dimen/font_normal" />
</LinearLayout>
项目地址 ☞ 传送门