简体   繁体   中英

How to update an EditText to convert input into currency? (Android Newbie)

I have a number of EditText's with an inputType="numberDecimal" and i'm wanting to change the input into currency (TextWatcher maybe?). Can anyone help with this? 99% of my project is done bar this. Thanks guys.

try below code:-

    tv.addTextChangedListener(new TextWatcher()
    {
        private String current = "";
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            if(!s.toString().equals(current))
            {
                if(tv.getText().toString().trim().length()>0)
                {
                    tv.removeTextChangedListener(this);
                    String formated = tv.getText().toString().trim().replace("$", "");
                    current = formated;
                    tv.setText("$"+formated);
                    tv.setSelection(formated.length()+1);   

                    tv.addTextChangedListener(this);
                }
            }
        }

         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after)
         {
         }
         @Override
         public void afterTextChanged(final Editable s)
         { 
         }
    });

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