自定義可輸入金額的EditText

@SuppressLint("AppCompatCustomView")
public class MoneyEditText extends EditText {
    private static final String TAG = "MoneyEditText";
    private boolean textChange;

    public MoneyEditText(Context context) {
        this(context,null);
    }

    public MoneyEditText(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MoneyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //設(shè)置可以輸入小數(shù)
        setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        setFocusable(true);
        setFocusableInTouchMode(true);

        //監(jiān)聽(tīng)文字變化
        addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!textChange) {
                    restrictText();
                }
                textChange = false;
            }
        });

    }

    /**
     * 將小數(shù)限制為2位
     */
    private void restrictText() {
        String input = getText().toString();
        if (TextUtils.isEmpty(input)) {
            return;
        }
        if (input.contains(".")) {
            int pointIndex = input.indexOf(".");
            int totalLenth = input.length();
            int len = (totalLenth - 1) - pointIndex;
            if (len > 2) {
                input = input.substring(0, totalLenth - 1);
                textChange = true;
                setText(input);
                setSelection(input.length());
            }
        }

        //處理以.為結(jié)束的格式
        if (input.toString().trim().substring(0).equals(".")) {
            input = "0" + input;
            setText(input);
            setSelection(2);
        }

        //處理0開(kāi)頭的數(shù)據(jù)
        if(input.length()>1 && !input.contains(".")){
            String substring1 = input.substring(0,1);
            String substring2 = input.substring(1,2);
            if(substring1.equals("0")){
                setText(substring2);
                setSelection(1);
            }
        }
    }

    /**
     * 獲取金額(請(qǐng)根據(jù)返回的金額是否為"0.00",來(lái)判斷是否為0金額)
     */
    public String getMoneyText() {
        String money = getText().toString();

        //如果最后一位是小數(shù)點(diǎn)
        if (money.endsWith(".")) {
            money = money.substring(0, money.length() - 1);
        }

        if("0".equals(money) || "0.0".equals(money) ){
           money = "0.00"; 
        }
        return money;
    }

}
image.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容