Android Edittext with auto detection of credit card type

后端 未结 3 1330
青春惊慌失措
青春惊慌失措 2021-02-04 17:31

I am dealing with payment option in android, By give an option to make a payment with credit card, some of the users may mistakenly choosing different credit card type for some

3条回答
  •  耶瑟儿~
    2021-02-04 17:50

    From the help of above answers and suggestion i have achieved the result,

    Here is the solution:

    Create a function for regex making

    public static ArrayList listOfPattern()
    {
        ArrayList listOfPattern=new ArrayList();
    
        String ptVisa = "^4[0-9]$";
    
        listOfPattern.add(ptVisa);
    
        String ptMasterCard = "^5[1-5]$";
    
        listOfPattern.add(ptMasterCard);
    
        String ptDiscover = "^6(?:011|5[0-9]{2})$";
    
        listOfPattern.add(ptDiscover);
    
        String ptAmeExp = "^3[47]$";
    
        listOfPattern.add(ptAmeExp);
    
        return listOfPattern;
    }
    
    Integer[] imageArray = { R.drawable.visa, R.drawable.master, R.drawable.disnet, R.drawable.ae };
    

    use this below code in addTextChangedListener

    creditcardnumberedittext.addTextChangedListener(new TextWatcher()
        {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                String ccNum = s.toString();
    
                if(ccNum.length()>=2)
                {
                    for (int i = 0; i < listOfPattern.size(); i++)
                    {
                        if (ccNum.substring(0, 2).matches(listOfPattern.get(i)))
                        {
                            creditcardnumberedittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, imageArray[i], 0);
    
                            cardtype = String.valueOf(i);
                        }
                    }
                }
    
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void afterTextChanged(Editable s)
            {               
    
                if (!creditcardnumberedittext.getText().toString().equalsIgnoreCase(""))
                {
                    for (int i = 0; i < listOfPattern.size(); i++)
                    {
                        if (creditcardnumberedittext.getText().toString().matches(listOfPattern.get(i)))
                        {
                            creditcardnumberedittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, imageArray[i], 0);
    
                            cardtype = String.valueOf(i);
                        }
                    }
                }
                else
                {
                    creditcardnumberedittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.allcards, 0);
                }
            }
        });
    

    Thanks..

提交回复
热议问题