本文介绍了如何创建效果隐藏数字(信用卡号中除最后3个数字以外的数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我需要一个带有验证的editText,并需要一个效果来隐藏信用卡号,最后三个数字除外.

我戴上了口罩,可以正常工作,但是我需要显示最后三个数字.

如何创建这种效果?

  <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" />

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

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;
    }
}
解决方案

我使用PasswordCharSequence解决了我的问题,并复制了一个蒙版

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

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?

  <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;
    }
}
解决方案

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

这篇关于如何创建效果隐藏数字(信用卡号中除最后3个数字以外的数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 07:37