<tfoot id='LytdE'></tfoot>

        <i id='LytdE'><tr id='LytdE'><dt id='LytdE'><q id='LytdE'><span id='LytdE'><b id='LytdE'><form id='LytdE'><ins id='LytdE'></ins><ul id='LytdE'></ul><sub id='LytdE'></sub></form><legend id='LytdE'></legend><bdo id='LytdE'><pre id='LytdE'><center id='LytdE'></center></pre></bdo></b><th id='LytdE'></th></span></q></dt></tr></i><div id='LytdE'><tfoot id='LytdE'></tfoot><dl id='LytdE'><fieldset id='LytdE'></fieldset></dl></div>

        <small id='LytdE'></small><noframes id='LytdE'>

      1. <legend id='LytdE'><style id='LytdE'><dir id='LytdE'><q id='LytdE'></q></dir></style></legend>

          <bdo id='LytdE'></bdo><ul id='LytdE'></ul>

        在 Android EditText 中限制小数位数

        时间:2023-05-28
        • <legend id='LvvEs'><style id='LvvEs'><dir id='LvvEs'><q id='LvvEs'></q></dir></style></legend>
            <tbody id='LvvEs'></tbody>

          <small id='LvvEs'></small><noframes id='LvvEs'>

              <bdo id='LvvEs'></bdo><ul id='LvvEs'></ul>

              1. <i id='LvvEs'><tr id='LvvEs'><dt id='LvvEs'><q id='LvvEs'><span id='LvvEs'><b id='LvvEs'><form id='LvvEs'><ins id='LvvEs'></ins><ul id='LvvEs'></ul><sub id='LvvEs'></sub></form><legend id='LvvEs'></legend><bdo id='LvvEs'><pre id='LvvEs'><center id='LvvEs'></center></pre></bdo></b><th id='LvvEs'></th></span></q></dt></tr></i><div id='LvvEs'><tfoot id='LvvEs'></tfoot><dl id='LvvEs'><fieldset id='LvvEs'></fieldset></dl></div>

                  <tfoot id='LvvEs'></tfoot>
                  本文介绍了在 Android EditText 中限制小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试编写一个可以帮助您管理财务的应用程序.我正在使用 EditText 字段,用户可以在其中指定金额.

                  I'm trying to write an app that helps you manage your finances. I'm using an EditText Field where the user can specify an amount of money.

                  我将 inputType 设置为 numberDecimal 效果很好,除了这允许人们输入诸如 123.122 之类的数字,这并不适合钱.

                  I set the inputType to numberDecimal which works fine, except that this allows people to enter numbers such as 123.122 which is not perfect for money.

                  有没有办法将小数点后的字符数限制为两个?

                  Is there a way to limit the number of characters after the decimal point to two?

                  推荐答案

                  这是一个示例 InputFilter,它只允许小数点前最多 4 位,小数点后最多 1 位.

                  Here is a sample InputFilter which only allows max 4 digits before the decimal point and max 1 digit after that.

                  edittext 允许的值:555.2555.2

                  Values that edittext allows: 555.2, 555, .2

                  编辑文本块的值:55555.2055.2555.42

                          InputFilter filter = new InputFilter() {
                          final int maxDigitsBeforeDecimalPoint=4;
                          final int maxDigitsAfterDecimalPoint=1;
                  
                          @Override
                          public CharSequence filter(CharSequence source, int start, int end,
                                  Spanned dest, int dstart, int dend) {
                                  StringBuilder builder = new StringBuilder(dest);
                                  builder.replace(dstart, dend, source
                                          .subSequence(start, end).toString());
                                  if (!builder.toString().matches(
                                          "(([1-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"
                  
                                          )) {
                                      if(source.length()==0)
                                          return dest.subSequence(dstart, dend);
                                      return "";
                                  }
                  
                              return null;
                  
                          }
                      };
                  
                      mEdittext.setFilters(new InputFilter[] { filter });
                  

                  这篇关于在 Android EditText 中限制小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Java 中将弧度转换为度数 下一篇:将光标放在 EditText 中文本的末尾

                  相关文章

                    <bdo id='cYxaJ'></bdo><ul id='cYxaJ'></ul>

                    <i id='cYxaJ'><tr id='cYxaJ'><dt id='cYxaJ'><q id='cYxaJ'><span id='cYxaJ'><b id='cYxaJ'><form id='cYxaJ'><ins id='cYxaJ'></ins><ul id='cYxaJ'></ul><sub id='cYxaJ'></sub></form><legend id='cYxaJ'></legend><bdo id='cYxaJ'><pre id='cYxaJ'><center id='cYxaJ'></center></pre></bdo></b><th id='cYxaJ'></th></span></q></dt></tr></i><div id='cYxaJ'><tfoot id='cYxaJ'></tfoot><dl id='cYxaJ'><fieldset id='cYxaJ'></fieldset></dl></div>
                      <legend id='cYxaJ'><style id='cYxaJ'><dir id='cYxaJ'><q id='cYxaJ'></q></dir></style></legend><tfoot id='cYxaJ'></tfoot>

                    1. <small id='cYxaJ'></small><noframes id='cYxaJ'>