想要格式化EditText中的数字

时间:2015-09-30 18:58:48

标签: android android-edittext textwatcher

我想格式化EditText中数字形式的输入。格式为 01-133134-124 。我希望在2个数字之后首先 - 然后在6个数字之后的下一个破折号。我试过但是每当我按下删除/退格时因为错误输入格式停止工作并且在输入2个数字后没有放置破折号或6个数字。这是代码。 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) {

    }
});

2 个答案:

答案 0 :(得分:3)

如果你改变一下你的方法会容易得多。在EditText的每次更改后,您都可以随时分析字符串中未格式化的版本。

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

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中间的数字,它会将光标移动到文本的末尾。)

答案 1 :(得分:0)

您可以使用(windows 7, jdk_1.7, tomcat)方法:

onTextChanged

此外,

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

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

 YourEditText.append("-"); }

改编自this文章。