Android EditText采用货币格式

时间:2016-08-15 07:04:35

标签: android currency-formatting

当我想输入100000时 然后它在EditText中将为100.000(自动遵循货币格式)。

这是代码

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LayoutInflater li = LayoutInflater.from(context);
            View promptsView = li.inflate(R.layout.prompt_add_items, null);

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);
            final EditText add_price = (EditText) promptsView.findViewById(R.id.add_iprice);
            setCurrency(add_price);

            itemModel= new ModelItems();
            _url = new Url();

            // set prompt_add_category.xmlcategory.xml to alertdialog builder
            alertDialogBuilder.setView(promptsView);

            // set dialog message
            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("Add",
                     new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                                    AddItem(add_price.getText().toString());
                    _url.setItems(itemModel);
            if (add_price.length() == 0) {
           Toast.makeText(getApplicationContext(), "All data must be filled.",Toast.LENGTH_LONG).show();
             } 
            else {
              new ParseUrlAsync();
                Itobjt = ParseUrl.getObjectResult(_url.getUrlItem());
                 if (Itobjt.getResult().equals("1")) {
                  Toast.makeText(getApplicationContext(), "Success to add item.",   Toast.LENGTH_LONG).show();
                   Log.e("URL: ",     _url.getUrlItem());

                    } else {
                     Toast.makeText(getApplicationContext(), "Cannot add item.", Toast.LENGTH_LONG).show();
                                        }
                                    }
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });
            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }
    });

private void setCurrency(final EditText edt) {
    edt.addTextChangedListener(new TextWatcher() {
        private String current = "";

        @Override
        public void afterTextChanged(Editable s) {

        }

        @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().equals(current)) {
                edt.removeTextChangedListener(this);

                Locale local = new Locale("id", "id");
                String replaceable = String.format("[Rp,.\\s]",
                        NumberFormat.getCurrencyInstance().getCurrency()
                                .getSymbol(local));
                String cleanString = s.toString().replaceAll(replaceable,
                        "");

                double parsed;
                try {
                    parsed = Double.parseDouble(cleanString);
                } catch (NumberFormatException e) {
                    parsed = 0.00;
                }

                NumberFormat formatter = NumberFormat
                        .getCurrencyInstance(local);
                formatter.setMaximumFractionDigits(0);
                formatter.setParseIntegerOnly(true);
                String formatted = formatter.format((parsed));

                String replace = String.format("[Rp\\s]",
                        NumberFormat.getCurrencyInstance().getCurrency()
                                .getSymbol(local));
                String clean = formatted.replaceAll(replace, "");

                current = formatted;
                edt.setText(clean);
               edt.setSelection(clean.length());
               edt.addTextChangedListener(this);
            }
        }
    });
}

但是在数据库中它将被保存为100并将显示为100。

如何仅在编辑文本中使用货币格式但在数据库中将节省100000? 请帮忙

0 个答案:

没有答案