简体   繁体   English

Android edittext限制输入的字符

[英]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. 我有一个编辑文本,允许用户输入6个字符,它会自动在字符3和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" 机器人:位数= “abcdefghijklmnopqrstuvwxyz1234567890”

This works but when i manually add the dash via textchange listener, it of course doesnt add it. 这有效但当我通过textchange监听器手动添加短划线时,它当然不会添加它。

So i added the dash on the above restrictions: 所以我在上述限制中添加了破折号:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890-" 机器人:位数= “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? 如何限制用户进入破折号,同时允许以编程方式将其添加到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 添加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(); 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; 添加类似boolean isManuallyAdded;的字段boolean isManuallyAdded; to your TextWatcher class. 到您的TextWatcher类。 Before you add the dash manually you set it to true . 在手动添加短划线之前,请将其设置为true Before deleting the user input you check that isManuallyAdded is false . 在删除用户输入之前,请检查isManuallyAdded是否为false If isManuallyAdded is true you reset it to false . 如果isManuallyAddedtrue ,则将其重置为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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