EditText输入小数位限制

本文介绍了在Android开发中如何自定义EditText控件,实现输入数值时限制小数位数的功能。通过示例代码展示了如何在布局文件中使用该自定义控件,并提供了项目源码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发过程中,往往会由于需求原因需要我们去定制一些符合要求的自定义控件,方便进行功能开发,其中小数位的输入限制便是最常见的功能之一。

效果图:

320*568

主要代码如下:

/**
 * @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>

项目地址 ☞ 传送门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值