Android Money Input with fixed decimal

后端 未结 7 1252
鱼传尺愫
鱼传尺愫 2020-12-02 00:07

How do you create an edittext entry that formats input in money format only? When the user enters 5, I want the input to look like \"$0.05\" and when they then enter 3, th

7条回答
  •  旧时难觅i
    2020-12-02 00:37

    Heres my complete solution:

                    tvValue.setRawInputType(Configuration.KEYBOARD_12KEY);
    
                tvValue.addTextChangedListener(new TextWatcher(){
    
    
                    @Override
                    public void afterTextChanged(Editable arg0) {
                        // TODO Auto-generated method stub
    
                    }
    
                    @Override
                    public void beforeTextChanged(CharSequence s, int start,
                            int count, int after) {
                        // TODO Auto-generated method stub
    
                    }
    
                    @Override
                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                        // TODO Auto-generated method stub
    
                        // here i converted to string
                        if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
                        {
                            String userInput= ""+s.toString().replaceAll("[^\\d]", "");
                            Float in=Float.parseFloat(userInput);
                            float percen = in/100;
                            tvValue.setText("$"+percen);
                        }
    
                    }
    
                });
    

提交回复
热议问题