android format edittext to display spaces after every 4 characters

拟墨画扇 提交于 2019-11-29 09:25:11

You need to use TextWatcher to achieve visual purpose spaces.

And use any simply split string by space logic to join it back or loop through the entire string per character wise and eliminate (char) 32 from the string

is this editext for credit card?
first create count variable

int count = 0;

then put this in your oncreate(activity) / onviewcreated(fragment)

    ccEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (count <= ccEditText.getText().toString().length()
                    &&(ccEditText.getText().toString().length()==4
                    ||ccEditText.getText().toString().length()==9
                        ||ccEditText.getText().toString().length()==14)){
                ccEditText.setText(ccEditText.getText().toString()+" ");
                int pos = ccEditText.getText().length();
                ccEditText.setSelection(pos);
            }else if (count >= ccEditText.getText().toString().length()
                    &&(ccEditText.getText().toString().length()==4
                    ||ccEditText.getText().toString().length()==9
                    ||ccEditText.getText().toString().length()==14)){
                ccEditText.setText(ccEditText.getText().toString().substring(0,ccEditText.getText().toString().length()-1));
                int pos = ccEditText.getText().length();
                ccEditText.setSelection(pos);
            }
            count = ccEditText.getText().toString().length();
        }
    });

as @waqas pointed out, you'll need to use a TextWatcher if your aim is to make this happen as the user types the number. Here is one potential way you could achieve the spaces:

StringBuilder s;
s = new StringBuilder(yourTxtView.getText().toString());

for(int i = 4; i < s.length(); i += 5){
    s.insert(i, " ");
}
yourTxtView.setText(s.toString());

Whenever you need to get the String without spaces do this:

String str = yourTxtView.getText().toString().replace(" ", "");

Format of text is 000 000 0000

android edittext textwatcher format phone number like xxx-xxx-xx-xx

public class PhoneNumberTextWatcher implements TextWatcher {

private static final String TAG = PhoneNumberTextWatcher.class
        .getSimpleName();
private EditText edTxt;
private boolean isDelete;

public PhoneNumberTextWatcher(EditText edTxtPhone) {
    this.edTxt = edTxtPhone;
    edTxt.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                isDelete = true;
            }
            return false;
        }
    });
}

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

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

public void afterTextChanged(Editable s) {

    if (isDelete) {
        isDelete = false;
        return;
    }
    String val = s.toString();
    String a = "";
    String b = "";
    String c = "";
    if (val != null && val.length() > 0) {
        val = val.replace(" ", "");
        if (val.length() >= 3) {
            a = val.substring(0, 3);
        } else if (val.length() < 3) {
            a = val.substring(0, val.length());
        }
        if (val.length() >= 6) {
            b = val.substring(3, 6);
            c = val.substring(6, val.length());
        } else if (val.length() > 3 && val.length() < 6) {
            b = val.substring(3, val.length());
        }
        StringBuffer stringBuffer = new StringBuffer();
        if (a != null && a.length() > 0) {
            stringBuffer.append(a);
            if (a.length() == 3) {
                stringBuffer.append(" ");
            }
        }
        if (b != null && b.length() > 0) {
            stringBuffer.append(b);
            if (b.length() == 3) {
                stringBuffer.append(" ");
            }
        }
        if (c != null && c.length() > 0) {
            stringBuffer.append(c);
        }
        edTxt.removeTextChangedListener(this);
        edTxt.setText(stringBuffer.toString());
        edTxt.setSelection(edTxt.getText().toString().length());
        edTxt.addTextChangedListener(this);
    } else {
        edTxt.removeTextChangedListener(this);
        edTxt.setText("");
        edTxt.addTextChangedListener(this);
    }

}
}

Here is a little help function. For your example you would call it with

addPadding(" ", "123456781234", 4);

/**
 * @brief Insert arbitrary string at regular interval into another string 
 * 
 * @param t String to insert every 'num' characters
 * @param s String to format
 * @param num Group size
 * @return
 */
private String addPadding(String t, String s, int num) {
    StringBuilder retVal;

    if (null == s || 0 >= num) {
        throw new IllegalArgumentException("Don't be silly");
    }

    if (s.length() <= num) {
        //String to small, do nothing
        return s;
    }

    retVal = new StringBuilder(s);

    for(int i = retVal.length(); i > 0; i -= num){
        retVal.insert(i, t);
    }
    return retVal.toString();
}

change the live text while typing is some what difficult. we should handle the following issues.

a. cursor position b. we should allow the user delete the entered text.

The following code handle both the issues.

  1. Add TextWatcher to EditText, and get the text from "afterTextchanged()" and write your logic

    String str=""; int strOldlen=0;

        @Override
                public void afterTextChanged(Editable s) {
    
       str = edtAadharNumber.getText().toString();
                    int strLen = str.length();
    
    
                    if(strOldlen<strLen) {
    
                        if (strLen > 0) {
                            if (strLen == 4 || strLen == 9) {
    
                                str=str+" ";
    
                                edtAadharNumber.setText(str);
                                edtAadharNumber.setSelection(edtAadharNumber.getText().length());
    
                            }else{
    
                                if(strLen==5){
                                    if(!str.contains(" ")){
                                     String tempStr=str.substring(0,strLen-1);
                                        tempStr +=" "+str.substring(strLen-1,strLen);
                                        edtAadharNumber.setText(tempStr);
                                        edtAadharNumber.setSelection(edtAadharNumber.getText().length());
                                    }
                                }
                                if(strLen==10){
                                    if(str.lastIndexOf(" ")!=9){
                                        String tempStr=str.substring(0,strLen-1);
                                        tempStr +=" "+str.substring(strLen-1,strLen);
                                        edtAadharNumber.setText(tempStr);
                                        edtAadharNumber.setSelection(edtAadharNumber.getText().length());
                                    }
                                }
                                strOldlen = strLen;
                            }
                        }else{
                            return;
                        }
    
                    }else{
                        strOldlen = strLen;
    
    
                        Log.i("MainActivity ","keyDel is Pressed ::: strLen : "+strLen+"\n old Str Len : "+strOldlen);
                    }
    
                }
    }
    
  2. Here I am trying to add space for every four characters. After adding first space, then the length of the text is 5. so next space is after 9 characters like that.

    if (strLen== 4||strLen==9)

    1. Here another problem is cursor position, once you modify the text of the edittext, the cursor move to first place. so we need to set the cursor manually.

    edtAadharNumber.setSelection(edtAadharNumber.getText().length());

    1. My text length is only 12 characters. So I am doing manual calculations, if your text is dynamic then you write dynamic logic.

cleaner version of @Ario's answer which follows the DRY principle:

private int prevCount = 0;
private boolean isAtSpaceDelimiter(int currCount) {
    return currCount == 4 || currCount == 9 || currCount == 14;
}

private boolean shouldIncrementOrDecrement(int currCount, boolean shouldIncrement) {
    if (shouldIncrement) {
        return prevCount <= currCount && isAtSpaceDelimiter(currCount);
    } else {
        return prevCount > currCount && isAtSpaceDelimiter(currCount);
    }
}

private void appendOrStrip(String field, boolean shouldAppend) {
    StringBuilder sb = new StringBuilder(field);
    if (shouldAppend) {
        sb.append(" ");
    } else {
        sb.setLength(sb.length() - 1);
    }
    cardNumber.setText(sb.toString());
    cardNumber.setSelection(sb.length());
}

ccEditText.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    } 

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

    } 

    @Override 
    public void afterTextChanged(Editable s) {
        String field = editable.toString();
        int currCount = field.length();

        if (shouldIncrementOrDecrement(currCount, true)){
            appendOrStrip(field, true);
        } else if (shouldIncrementOrDecrement(currCount, false)) {
            appendOrStrip(field, false);
        }
        prevCount = cardNumber.getText().toString().length(); 
    } 
}); 

Simple Answer

    YourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

            int len=s.toString().length();

            if (before == 0 && (len == 4 || len == 9 || len == 14 ))
                YourEditText.append(" ");
        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });

Assuming that you know the final length of the String, you could implement a TextWatcher this way:

override fun setUp(view: View?) {

    editText.addTextChangedListener(object : TextWatcher{
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun onTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {
            if(p2 == 0 && (p0.length == 4 || p0.length == 9 || p0.length == 14))
                editText.append(" ")
        }

        override fun afterTextChanged(p0: Editable?) {
        }
    })

You just add a space each 4-digits block. p2 == 0 is to assure the user is not deleting, otherwise he/she would get stock.

The code is in Kotlin, You can do it exactly the same way in Java.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!