天天看點

Android自定義價格輸入框EditText

public class PriceEditText extends EditText {
    /**
     * 小數點前的價格長度
     * 123456789.00 價格長度為 9
     */
    private final int PRICE_LENGTH = ;
    private CharSequence defaultHint;
    private DecimalFormat decimalFormat;
    private String hint="輸入資料過大,請重新輸入";

    public PriceEditText(Context context) {
        super(context);
        init();
    }

    public PriceEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PriceEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        /**
         * 設定輸入類型:數字和小數點
         */
        setInputType(EditorInfo.TYPE_CLASS_NUMBER | EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
        setMaxLines();
        setFilters(new InputFilter[]{new InputFilter.LengthFilter(PRICE_LENGTH + )});
        defaultHint = getHint();

        addTextChangedListener(new TextWatcher() {

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

                //判斷第一位輸入是否是“.”
                if (s.toString().startsWith(".")) {
                    s = "0" + s;
                    setText(s);
                    if (s.toString().length() == ) {
                        setSelection(s.length());
                    }
                    return;
                }
                //判斷首位是否是“0”
                if (s.toString().startsWith("0") && s.toString().length() > ) {
                    //判斷第二位不是“.”
                    if (!s.toString().substring(, ).equals(".")) {

                        s = s.toString().substring(, s.toString().length());
                        setText(s);
                        setSelection(s.length());
                        return;
                    }
                }


                //判斷小數點後兩位
                if (s.toString().contains(".")) {
                    if (s.length() -  - s.toString().indexOf(".") > ) {
                        s = s.toString().subSequence(, s.toString().indexOf(".") + );
                        setText(s);
                        setSelection(s.length());
                        return;
                    }
                }

                //判斷輸入位數
                if (s.toString().length() > PRICE_LENGTH) {
                    if (s.toString().contains(".")) {
                        if (s.toString().indexOf(".") > PRICE_LENGTH) {
                            setText("");
                            setHint(hint);
                            setSelection();
                            return;
                        }
                    } else if (s.toString().length() == PRICE_LENGTH + ) {
                        if (start +  == s.toString().length()) {
                            s = s.toString().subSequence(, PRICE_LENGTH);
                            setText(s);
                            setSelection(s.length());
                            return;
                        } else {
                            setText("");
                            setHint(hint);
                            setSelection();
                            return;
                        }
                    } else if (s.toString().length() > PRICE_LENGTH + ) {
                        setText("");
                        setHint(hint);
                        setSelection();
                        return;
                    }
                }

                //還原提示内容
                if (defaultHint != getHint()) {
                    setHint(defaultHint);
                }
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    /**
     * 擷取double類型的價格
     *
     * @return
     */
    public double getDoublePrice() {
        String s = getText().toString();
        if (TextUtils.isEmpty(s)) return ;
        return Double.parseDouble(s);

    }

    /**
     * 擷取string類型的價格
     *
     * @return
     */
    public String getStringPrice() {
        String s = getText().toString();
        if (TextUtils.isEmpty(s)) return "0.00";
        if (decimalFormat == null) decimalFormat = new DecimalFormat("#0.00");
        return decimalFormat.format(Double.parseDouble(s));

    }

    /**
     * 擷取string類型的價格(帶¥符号)
     *
     * @return
     */
    public String getStringYuan() {
        String s = getText().toString();
        if (TextUtils.isEmpty(s)) return "¥:0.00";
        if (decimalFormat == null) decimalFormat = new DecimalFormat("#0.00");
        return "¥:" + decimalFormat.format(Double.parseDouble(s));

    }

    /**
     * 設定價格
     * @param text
     */
    public void setPrice(String text) {
        double price = ;
        try {

            price = Double.parseDouble(text);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        setPrice(price);
    }
    /**
     * 設定價格
     * @param d
     */
    public void setPrice(Double d) {
        if (decimalFormat == null) decimalFormat = new DecimalFormat("#0.00");
        setText(decimalFormat.format(d));
    }
    /**
     * 設定價格
     * @param l
     */
    public void setPrice(long l) {
        if (decimalFormat == null) decimalFormat = new DecimalFormat("#0.00");
        setText(decimalFormat.format(l));
    }
}