格式价格0.00格式使用的EditText机器人 [英] Format Price in 0.00 format in android using edittext

查看:87
本文介绍了格式价格0.00格式使用的EditText机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图掩盖价格值,使得它总是在 0.00 格式。这是我的code

I am trying to mask a price value so that it is always in 0.00 format. Here is my code

float temp = 0;

DecimalFormat mDecimalFormat = new DecimalFormat("###.00");

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

    if(s.toString()!=null)
    {

        mMasEditText.removeTextChangedListener(this);
        temp = Float.parseFloat(s.toString());
        mMasEditText.setText(""+mDecimalFormat.format(addNumber(temp)));
        mMasEditText.setSelection(start+1);
        mMasEditText.addTextChangedListener(this);

    }
}

public float addNumber(float numTemp){
    float result=0.00f;
    result = numTemp + result;
    return result;
}

但是,当我pressing小数,我希望光标去一个步骤。但我没能得到点回调。此外,当我pressing后退按钮,以消除数字,我得到一个索引出界异常。谁能告诉我怎么去onTextChanged听众面前后退按钮和圆点按钮回调?

But when I am pressing decimal, I want the cursor to go to one more step. But I am not able to get the dot callback. Also when I am pressing the back button, to remove digits, I get an index out of bound exception. Can anyone tell me how to get the back button and dot button callback before onTextChanged listener?

推荐答案

使用的String.Format

  String.format("$%.2f", result);

   Strint text = String.format("$%.2f", addNumber(temp));
   mMasEditText.setText(text)

这篇关于格式价格0.00格式使用的EditText机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆