Android EditText-限制输入小数位数

野性酷女 2023-10-03 18:22 22阅读 0赞

在一些要输入金额的输入框中,通常会有限制输入小数位数的要求,提供一个用TextWatcher实现的方式。
输入规则:
1.输入0时,不再进行输入
2.首位输入小数点时,小数点前自动补0
3.超出小数限制位数,禁止继续输入
4.超出总限制字符数,禁止输入


  1. import android.text.Editable;
  2. import android.text.InputFilter;
  3. import android.text.TextWatcher;
  4. import android.widget.EditText;
  5. /**
  6. * Description: 小数位数限定
  7. */
  8. public class DecimalInputTextWatcher implements TextWatcher {
  9. private static final String Period = ".";
  10. private static final String Zero = "0";
  11. /**
  12. * 需要设置该 DecimalInputTextWatcher 的 EditText
  13. */
  14. private EditText editText = null;
  15. /**
  16. * 默认 小数的位数 2 位
  17. */
  18. private static final int DEFAULT_DECIMAL_DIGITS = 2;
  19. private int decimalDigits;// 小数的位数
  20. private int totalDigits;//最大长度
  21. /**
  22. * @param editText editText
  23. * @param totalDigits 最大长度
  24. * @param decimalDigits 小数的位数
  25. */
  26. public DecimalInputTextWatcher(EditText editText, int totalDigits, int decimalDigits) {
  27. if (editText == null) {
  28. throw new RuntimeException("editText can not be null");
  29. }
  30. this.editText = editText;
  31. if (totalDigits <= 0)
  32. throw new RuntimeException("totalDigits must > 0");
  33. if (decimalDigits <= 0)
  34. throw new RuntimeException("decimalDigits must > 0");
  35. this.totalDigits = totalDigits;
  36. this.decimalDigits = decimalDigits;
  37. }
  38. @Override
  39. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  40. }
  41. @Override
  42. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  43. }
  44. @Override
  45. public void afterTextChanged(Editable editable) {
  46. try {
  47. String s = editable.toString();
  48. editText.removeTextChangedListener(this);
  49. //限制最大长度
  50. editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(totalDigits)});
  51. if (s.contains(Period)) {
  52. //超过小数位限定位数,只保留限定小数位数
  53. if (s.length() - 1 - s.indexOf(Period) > decimalDigits) {
  54. s = s.substring(0,
  55. s.indexOf(Period) + decimalDigits + 1);
  56. editable.replace(0, editable.length(), s.trim());
  57. }
  58. }
  59. //如果首位输入"."自动补0
  60. if (s.trim().equals(Period)) {
  61. s = Zero + s;
  62. editable.replace(0, editable.length(), s.trim());
  63. }
  64. //首位输入0时,不再继续输入
  65. if (s.startsWith(Zero)
  66. && s.trim().length() > 1) {
  67. if (!s.substring(1, 2).equals(Period)) {
  68. editable.replace(0, editable.length(), Zero);
  69. }
  70. }
  71. editText.addTextChangedListener(this);
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. }

使用时:
edit需要先限定输入方式:

9240d6b182a09dc2c15da7e57dbb5799.png

调用:

  1. EditText numberDecimalEt = findViewById(R.id.number_decimal_et);
  2. //输入总长度15位,小数2位
  3. numberDecimalEt.addTextChangedListener(new DecimalInputTextWatcher(numberDecimalEt, 15, 2));

作者:Crazy_Max
链接:https://www.jianshu.com/p/2e07d7cb21f7
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发表评论

表情:
评论列表 (有 0 条评论,22人围观)

还没有评论,来说两句吧...

相关阅读