在Android中自动添加在EDITTEXT破折号 [英] Adding a Dash in the editText automatically in Android

查看:213
本文介绍了在Android中自动添加在EDITTEXT破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个在我的codeS:

Have a look at my codes:

txt_HomeNo.addTextChangedListener(new TextWatcher() {

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

            boolean flag = true;
            String eachBlock[] = txt_HomeNo.getText().toString().split("-");
            for (int i = 0; i < eachBlock.length; i++) {
                if (eachBlock[i].length() > 3) {
                    flag = false;
                }
            }

            if (flag) {

                txt_HomeNo.setOnKeyListener(new OnKeyListener() {

                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {

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

                if (keyDel == 0) {

                    if (((txt_HomeNo.getText().length() + 1) % 4) == 0) {

                        if (txt_HomeNo.getText().toString().split("-").length <= 3) {
                            txt_HomeNo.setText(txt_HomeNo.getText() + "-");
                            txt_HomeNo.setSelection(txt_HomeNo.getText().length());
                        }
                    }
                    a = txt_HomeNo.getText().toString();
                } else {
                    a = txt_HomeNo.getText().toString();
                    keyDel = 0;
                }

            } else {
                txt_HomeNo.setText(a);
            }

        }

电话号码的最大长度只有7。当我已经输入3位数字,其追加破折号(这是我想发生什么事),但我在这里的问题是,在接下来的3个数字还会追加冲刺<强>(像这样: 511-871 - ) ...我的问题是,我该怎么办下一个4位数字的编码将与有破折号它。请帮我这个。谢谢!

The maximum length of the phone number is only 7. And when I already inputted 3 digits, it appends dash (that's what I'd like to happen) but my problem here is that the next 3 digits also appends dash (Like this: 511-871-)... My question is how can I do the codings with the next 4 digit number with having a dash on it. Please help me with this. thanks!

推荐答案

试试这个

@Override
public void afterTextChanged(Editable text) {     


    if (text.length() == 3 || text.length() == 7) {
        text.append('-');
    }


}

或所有这一切

private boolean isFormatting;
private boolean deletingHyphen;
private int hyphenStart;
private boolean deletingBackward;

@Override
public void afterTextChanged(Editable text) {
    if (isFormatting)
        return;

    isFormatting = true;

    // If deleting hyphen, also delete character before or after it
    if (deletingHyphen && hyphenStart > 0) {
        if (deletingBackward) {
            if (hyphenStart - 1 < text.length()) {
                text.delete(hyphenStart - 1, hyphenStart);
            }
        } else if (hyphenStart < text.length()) {
            text.delete(hyphenStart, hyphenStart + 1);
        }
    }
    if (text.length() == 3 || text.length() == 7) {
        text.append('-');
    }

    isFormatting = false;
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    if (isFormatting)
        return;

    // Make sure user is deleting one char, without a selection
    final int selStart = Selection.getSelectionStart(s);
    final int selEnd = Selection.getSelectionEnd(s);
    if (s.length() > 1 // Can delete another character
            && count == 1 // Deleting only one character
            && after == 0 // Deleting
            && s.charAt(start) == '-' // a hyphen
            && selStart == selEnd) { // no selection
        deletingHyphen = true;
        hyphenStart = start;
        // Check if the user is deleting forward or backward
        if (selStart == start + 1) {
            deletingBackward = true;
        } else {
            deletingBackward = false;
        }
    } else {
        deletingHyphen = false;
    }
}

这篇关于在Android中自动添加在EDITTEXT破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆