简体   繁体   中英

Unable to add decimal like 0.25 with EditText Android

I have an app with editText and I want to insert decimal numbers that start from (0.25-0.50-0.75 and so on) because I want to calculate it with other editText numbers, till now I can only insert numbers like 1-2-3... Any idea please?

MyCode

price_per_piece.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

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

            @SuppressLint("DefaultLocale")
            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    //case_qty = v.findViewById(R.id.et_add_item_case_quanitiy); this line for understanding
                    //Note : case_qty is EditText which i want to insert decimal numbers
                    String qty_txt = case_qty.getText().toString();
                    if (qty_txt.equals("")){
                        qty_txt = "1";
                       // qty_txt = "0.25";
                    }
                    float qty = Float.parseFloat(qty_txt);
                    float p = Float.valueOf(s.toString());
                    price_per_case.setText(String.valueOf(qty * p));
                }
            }
        });

Thanks in Advance

Add android:inputType="numberDecimal" to your EditText xml declaration if you have not done so already. Then try use Double.parseDouble() instead of the float

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM