How can I create effect hide numbers the credit card except the last 3 numbers

后端 未结 1 423
渐次进展
渐次进展 2020-12-25 09:07

I have a problem, I need a editText with validation and a effect hide numbers the credit card except the last 3 numbers..

I have the mask and work fine, but I need

相关标签:
1条回答
  • 2020-12-25 09:56

    I solved my problem using PasswordCharSequence and aplicate a Mask

    public class TarjetasBancarias extends AppCompatActivity {
    
    private EditText creditCard;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tarjetas_bancarias);
    
        creditCard  = (EditText)findViewById(R.id.creditCard);
        creditCard.setTransformationMethod(new MyPasswordTransformationMethod());
         creditCard.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                boolean flag = true;
                String eachBlock[] = creditCard.getText().toString().split("-");
                for (int i = 0; i < eachBlock.length; i++) {
                    if (eachBlock[i].length() > 4) {
                        flag = false;
                    }
                }
                if (flag) {
    
                    creditCard.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) {
    
                        if (((creditCard.getText().length() + 1) % 5) == 0) {
    
                            if (creditCard.getText().toString().split("-").length <= 3) {
                                creditCard.setText(creditCard.getText() + "-");
                                creditCard.setSelection(creditCard.getText().length());
                            }
                        }
                        a = creditCard.getText().toString();
                    } else {
                        a = creditCard.getText().toString();
                        keyDel = 0;
                    }
    
                } else {
                    creditCard.setText(a);
                }
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
        });
    
    }
    
    
    public static class MyPasswordTransformationMethod extends PasswordTransformationMethod {
        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            return new PasswordCharSequence(source);
        }
    
        private class PasswordCharSequence implements CharSequence {
            private CharSequence mSource;
            public PasswordCharSequence(CharSequence source) {
                mSource = source;
            }
            public char charAt(int index) {
             char caracter;
               switch (index)
                {
                    case 4:
                        caracter = ' ';
                        break;
                    case 9:
                        caracter = ' ';
                        break;
                    case 14:
                        caracter = ' ';
                        break;
                    default:
                        if(index < 15)
                            return '*';
                        else
                            caracter = mSource.charAt(index);
                        break;
    
    
    
                }
    
    
                return caracter;
            }
            public int length() {
                return mSource.length();
            }
            public CharSequence subSequence(int start, int end) {
                return mSource.subSequence(start, end); 
            }
        }
    };}
    

    0 讨论(0)
提交回复
热议问题