简体   繁体   中英

Android edittext restrict characters entered

i have a edit text that allows a user to enter 6 characters and it automatically adds a dash in between characters 3 and 4.

i want to restrict the user from manually entering the dash or any other special characters on the edit text and i have done this below:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890"

This works but when i manually add the dash via textchange listener, it of course doesnt add it.

So i added the dash on the above restrictions:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890-"

And of course the user can now enter the dash!

How can i restrict the user from entering the dash whilst allowing it to be programatically added to the editText?

current code on the text change listener

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

    // add dash when user enters 4th character
    if (text.length() == 4 && text.length() > before) {
        text = (text.subSequence(0, 3) + "-" + text.charAt(count - 1));
        int pos = text.length();
        editText.setText(text);
        editText.setSelection(pos);

    } else if (text.length() == 4 && text.length() < before) {
        // delete dash when user presses back
        editText.setText(text.subSequence(0, 3));

        editText.setSelection(text.length() - 1);
    }

}

Add keyListener

edtxt.setKeyListener(new AlphaKeyListner());

public class AlphaKeyListner extends NumberKeyListener
{
    @Override
    protected char[] getAcceptedChars()
    {       
        return new char [] { 
                             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
                             'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
                             'u', 'v', 'w', 'x', 'y', 'z', 
                             'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
                             'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
                             'U', 'V', 'W', 'X', 'Y', 'Z',
                              '1','2','3','4','5','6','7','8','9','0'};
    }

    @Override
    public void clearMetaKeyState(View view, Editable content, int states)
    {

    }

    @Override
    public int getInputType()
    {   
        return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
    }   
}

StringBuilder sb=new StringBuilder();

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

    // add dash when user enters 4th character
    if (sb.length() == 3) {
        sb.append("-");
        sb.append(text)
        editText.setText(sb.toString());
        editText.setSelection(pos);

    } else{
        sb.append(text)
        editText.setText(sb.toString());
        editText.setSelection(pos); 
    }

}

在添加短划线之前更改允许的数字并在之后更改它们时,它可能会有所帮助。

Add a field like boolean isManuallyAdded; to your TextWatcher class. Before you add the dash manually you set it to true . Before deleting the user input you check that isManuallyAdded is false . If isManuallyAdded is true you reset it to false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM