java [一个简单的EditText视图,用于信用卡。它在右侧显示信用卡的图像。 #Credit Cards] #tags:Android

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java [一个简单的EditText视图,用于信用卡。它在右侧显示信用卡的图像。 #Credit Cards] #tags:Android相关的知识,希望对你有一定的参考价值。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.util.SparseArray;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Explaination of the code can be found on my medium account:
 * https://medium.com/@ali.muzaffar/android-developers-create-your-own-credit-card-edittext-view-e508c758e86c
 * @author Ali Muzaffar (http://alimuzaffar.com/)
 */
public class CreditCardEditText extends AppCompatEditText {

  private SparseArray<Pattern> mCCPatterns = null;

  private final char mSeparator = ' ';

  private final int mDefaultDrawableResId = R.drawable.creditcard; //default credit card image
  private int mCurrentDrawableResId = 0;
  private Drawable mCurrentDrawable;

  public CreditCardEditText(Context context) {
    super(context);
    init();
  }

  public CreditCardEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public CreditCardEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    if (mCCPatterns == null) {
      mCCPatterns = new SparseArray<>();
      // Without spaces for credit card masking
      mCCPatterns.put(R.drawable.visa, Pattern.compile("^4[0-9]{2,12}(?:[0-9]{3})?$"));
      mCCPatterns.put(R.drawable.mastercard, Pattern.compile("^5[1-5][0-9]{1,14}$"));
      mCCPatterns.put(R.drawable.amex, Pattern.compile("^3[47][0-9]{1,13}$"));
    }
  }

  @Override
  protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {

    if (mCCPatterns == null) {
      init();
    }

    int mDrawableResId = 0;
    for (int i = 0; i < mCCPatterns.size(); i++) {
      int key = mCCPatterns.keyAt(i);
      // get the object by the key.
      Pattern p = mCCPatterns.get(key);

      Matcher m = p.matcher(text);
      if (m.find()) {
        mDrawableResId = key;
        break;
      }
    }
    if (mDrawableResId > 0 && mDrawableResId != mCurrentDrawableResId) {
      mCurrentDrawableResId = mDrawableResId;
    } else if (mDrawableResId == 0) {
      mCurrentDrawableResId = mDefaultDrawableResId;
    }

    mCurrentDrawable = getResources().getDrawable(mCurrentDrawableResId);

  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mCurrentDrawable == null) {
      return;
    }

    int rightOffset = 0;
    if (getError() != null && getError().length() > 0) {
      rightOffset = (int) getResources().getDisplayMetrics().density * 32;
    }

    int right = getWidth() - getPaddingRight() - rightOffset;

    int top = getPaddingTop();
    int bottom = getHeight() - getPaddingBottom();
    float ratio = (float) mCurrentDrawable.getIntrinsicWidth() / (float) mCurrentDrawable.getIntrinsicHeight();
    //int left = right - mCurrentDrawable.getIntrinsicWidth(); //If images are correct size.
    int left = (int) (right - ((bottom - top) * ratio)); //scale image depeding on height available.
    mCurrentDrawable.setBounds(left, top, right, bottom);

    mCurrentDrawable.draw(canvas);

  }
}

以上是关于java [一个简单的EditText视图,用于信用卡。它在右侧显示信用卡的图像。 #Credit Cards] #tags:Android的主要内容,如果未能解决你的问题,请参考以下文章

列表视图edittext数据在Android中滚动时丢失

如何使用自定义列表视图在edittext过滤器中获取空间?可以使用简单的适配器吗?

Android 中通过Java代码动态生成EditText视图,并循环遍历EditText取出遍历的ID和文本框的值

EditText setSelection 不适用于大于 8 的位置

Android:EditText 导致内存泄漏

在Android Java中将EditText的提示移到顶部[重复]