【问题标题】:Android Edittext with auto detection of credit card type具有自动检测信用卡类型的 Android Edittext
【发布时间】:2015-02-04 05:40:22
【问题描述】:

我正在处理android中的付款选项,通过提供使用信用卡付款的选项,一些用户可能会错误地为其他信用卡号码选择不同的信用卡类型,所以我决定看看这个问题是根据输入的号码自动检测卡类型,

我发现 Flipkart 已经在他们的 android 应用程序中提供了这个功能,在这里我附上了这个功能,

如何在更改卡片类型时像动画一样做到这一点?

我知道要创建一个文本观察器,并根据输入使用 EditText 的 drawableRight 图像更改卡片类型。

但需要在 drawableRight 上做一些动画。

帮助表示赞赏..!

【问题讨论】:

标签: android autocomplete android-edittext


【解决方案1】:

在上述答案和建议的帮助下,我已经取得了结果,

解决办法如下:

为正则表达式创建一个函数

public static ArrayList<String> listOfPattern()
{
    ArrayList<String> listOfPattern=new ArrayList<String>();

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

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

谢谢..

【讨论】:

    【解决方案2】:

    您将在输入文本时查看文本,检查前几位数字以匹配此处找到的代码:

    http://www.stevemorse.org/ssn/List_of_Bank_Identification_Numbers.html

    这可以通过 TextWatcher 侦听器 Murtaza Hussain 指出的来完成。

    通过以下方式检查文本:

    switch (s.charAt(0)){
    case '4':
        // show visa symbol
        break;
    case '5':
        // show mastercard symbol
        break;
    ...
    

    【讨论】:

    • 感谢您的链接,这有助于我制作正则表达式。
    【解决方案3】:

    没有本机功能。您必须在EditText 上设置TextWatcher 类。它的作用是在您的字段上设置一个侦听器,并在您键入时为您提供检查输入的方法。

    yourEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                    //Check your Credit Card Number here.
    
                }
    
    
       });
    

    【讨论】:

    • 这取决于你的实现,你也可以使用FlipAnimator 来翻转视图。但是,如果您只是在寻找不好的代码。尝试使用上述技术。
    • 注:也可以将SlideUpSlideDown动画定义为FlipAnimator
    • 我不是在寻找确切的代码,但我的问题是为整个列表制作动画。
    • 这个技巧我已经解释过了,你可以试试。
    • @MurtazaKhursheedHussain 请给出一些详细的答案来回答这个问题。问题是关于如何获得与卡号相对应的不同卡图像。您的答案只显示了一个 addTextChangedListener。
    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多