让 EditText 只显示两位小数

我想在EditText 中只显示两位小数。我想在edit text中显示currency,但是想限制数在小数点后两位。

我不想用正则表达式这个方法。我查找的资料说java支持一些internal library函数能实现这个功能。请大家帮忙给点建议。

amount.setRawInputType(Configuration.KEYBOARD_12KEY);
         amount.addTextChangedListener(new TextWatcher() 
         {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                DecimalFormat formatVal = new DecimalFormat("##.##");
                String formatted = formatVal.format(s);
                amount.setText(formatted);
            }

你可以简单地使用 DecimalFormat

DecimalFormat format = new DecimalFormat("##.##");
String formatted = format.format(22.123);
editText.setText(formatted);

就会在 EditText 获得 22.12的结果。

  EditText.addTextChangedListener(new TextWatcher() 
  {
      public void afterTextChanged(Editable edt) 
      {
          String temp = edt.toString();
          int posDot = temp.indexOf(".");
          if (posDot <= 0) return;
          if (temp.length() - posDot - 1 > 2)
          {
              edt.delete(posDot + 3, posDot + 4);
          }
      }

      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
  });

以前看到过的例子