【问题标题】:Remove zeros from first of numbers in Android EditText从 Android EditText 中的第一个数字中删除零
【发布时间】:2020-10-06 10:30:33
【问题描述】:

在 Android EditText 中,inputType 是数字。我不想在其他数字之前有零。我的意思是当用户输入 0 然后输入 3 时,它会显示 03 但我想要 3。我知道我可以使用 textwatcher。但是有更好的解决方案吗?!

【问题讨论】:

  • 你不能使用 addTextChangedListener 吗?
  • 我可以使用任何特殊的 inputType 来执行此操作吗? @AjayKS

标签: android android-edittext android-inputtype


【解决方案1】:

还有另一种方法可以使用 textWatcher 。在 afterTextChange 方法中检查可编辑文本的长度。如果长度为 1 且字符串为 0,则删除 0 。

编辑:

editTextPack.addTextChangedListener(new TextWatcher() {

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

                        if (s.toString().trim().length() == 1 && s.toString().trim().equals("0")) {

                        editTextPack.setText("");                             
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count,
                                                  int after) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        // TODO Auto-generated method stub
                    }
                });

类似的东西。

【讨论】:

  • 003 或 0003 呢?!
  • 使用 afterTextChange 他将不能写超过一个零
  • 看我的编辑,你可以在 onTextChanged 或 afterTextChanged 里面做
猜你喜欢
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 2017-11-24
  • 1970-01-01
相关资源
最近更新 更多