Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

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

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 show the last 3 numbers.

How can I create this effect?

enter image description here

  <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/creditCard"
        android:layout_below="@+id/CVV"
        android:inputType="number"
        android:digits=" 1234567890"
        android:maxLength="16"
        android:layout_alignParentStart="true" />

class TarjetasBancarias

public class TarjetasBancarias extends AppCompatActivity {
    String a;
    int keyDel;
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.addTextChangedListener(new CreditCardNumberFormattingTextWatcher());
}

class CreditCardNumberFormattingTextWatcher

public static class CreditCardNumberFormattingTextWatcher implements TextWatcher {

    private boolean lock;

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

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

    @Override
    public void afterTextChanged(Editable s) {
        if (lock || s.length() > 16) {
            return;
        }
        lock = true;
        for (int i = 4; i < s.length(); i += 5) {
            s.insert(i, "*");
            if (s.toString().charAt(i) != ' ') {
                s.insert(i, " ");
            }
        }
        lock = false;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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); 
        }
    }
};}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...