想要在 EditText 中格式化数字

Want to format the number in EditText

我想将EditText.The格式的数字形式的输入格式化为01-133134-124。我想先 - 在 2 个数字之后然后在 6 numbers.I 之后尝试下一个破折号但是每当我按 delete/backspace 因为输入错误格式停止工作并且没有破折号是放置在输入 2 个数字或 6 个之后 numbers.Here 是 code.Where Enrollement 是 EditText 字段。

格式: 2digitnumber-6dignumber-3dignumber

Enrollement.addTextChangedListener(new TextWatcher() {

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

        Enrollement.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_DEL)
                    keyDel = 1;
                return false;
            }
        });

        if (keyDel == 0) {
            int len = Enrollement.getText().length();
            if (len == 2) {
                int leng = Enrollement.getText().length();
                if(leng==2) {
                    Enrollement.setText(Enrollement.getText() + "-");
                    Enrollement.setSelection(Enrollement.getText().length());
                }
                } else if (len == 9) {
                int leng = Enrollement.getText().length();
                if(leng==9) {
                    Enrollement.setText(Enrollement.getText() + "-");
                    Enrollement.setSelection(Enrollement.getText().length());
                }
            }
        } else {
            keyDel = 0;
        }
    }

    @Override
    public void afterTextChanged(Editable arg0) {

    }

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

    }
});

如果你稍微改变一下你的方法会容易得多。在 EditText.

中的每一次更改后,您可能总是在分析字符串的未格式化版本

试试这个(记得为您的 EditText 设置 android:maxLength="13"):

Enrollement.addTextChangedListener(new TextWatcher() {

    final int[] blockLengths = new int[]{2, 6, 3};
    String mUnformatted = "";

    @Override
    public void onTextChanged (CharSequence s,int start, int before, int count){
        String unformattedSeq = s.toString().replace("-", "");
        if (mUnformatted.length() == unformattedSeq.length()) {
            return; //length of text has not changed
        }

        mUnformatted = unformattedSeq;
        //formatting sequence
        StringBuilder formatted = new StringBuilder();
        int blockIndex = 0;
        int currentBlock = 0;
        for (int i = 0; i < mUnformatted.length(); ++i) {
            if (currentBlock == blockLengths[blockIndex]) {
                formatted.append("-");
                currentBlock = 0;
                blockIndex++;
            }
            formatted.append(mUnformatted.charAt(i));
            currentBlock++;
        }

        Enrollement.setText(formatted.toString());
        Enrollement.setSelection(formatted.length());
    }

    @Override
    public void beforeTextChanged (CharSequence s,int start, int count, int after){
    }

    @Override
    public void afterTextChanged (Editable s){
    }
});

这段代码应该可以正常工作。您只能改进 setSelection 部分(目前,如果您删除 EditText 中间的数字,它会将光标移动到文本的末尾)。

在您的 onTextChanged 方法中,您可以执行以下操作:

String str = YourEditText.getText().toString();

if((str.length()==2 && len <str.length()) || (str.length()==6 && len <str.length())){

 YourEditText.append("-"); }

此外,

   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
     int after) {
    // TODO Auto-generated method stub
    String str = YourEditText.getText().toString();
    len = str.length();
   }

改编自 this 文章。