From 774b48a6b684b1919905dfee34eee6a9fbb15c71 Mon Sep 17 00:00:00 2001 From: maning Date: Wed, 25 Jul 2018 09:38:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E8=B0=83=E7=9B=91=E5=90=AC=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mnpasswordedittext/MainActivity.java | 10 +- app/src/main/res/layout/activity_main.xml | 39 +++- .../MNPasswordEditText.java | 180 ++++++++++++------ .../src/main/res/values/attr.xml | 1 - 4 files changed, 155 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/com/maning/mnpasswordedittext/MainActivity.java b/app/src/main/java/com/maning/mnpasswordedittext/MainActivity.java index 713021a..41d069d 100644 --- a/app/src/main/java/com/maning/mnpasswordedittext/MainActivity.java +++ b/app/src/main/java/com/maning/mnpasswordedittext/MainActivity.java @@ -3,6 +3,7 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; +import android.widget.Toast; import com.maning.pswedittextlibrary.MNPasswordEditText; @@ -18,10 +19,13 @@ protected void onCreate(Bundle savedInstanceState) { tvShow = (TextView) findViewById(R.id.tvShow); mPswEditText = (MNPasswordEditText) findViewById(R.id.mPswEditText); - mPswEditText.setOnPasswordChangeListener(new MNPasswordEditText.OnPasswordChangeListener() { + mPswEditText.setOnTextChangeListener(new MNPasswordEditText.OnTextChangeListener() { @Override - public void onPasswordChange(String password) { - tvShow.setText(password); + public void onTextChange(String text, boolean isComplete) { + tvShow.setText(text); + if (isComplete) { + Toast.makeText(MainActivity.this, "输入完成", Toast.LENGTH_SHORT).show(); + } } }); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 328f3e3..539f534 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -18,6 +18,8 @@ android:id="@+id/mPswEditText" android:layout_width="match_parent" android:layout_height="50dp" + android:layout_marginLeft="16dp" + android:layout_marginRight="16dp" android:layout_marginTop="10dp" android:focusableInTouchMode="true" android:inputType="number" @@ -35,11 +37,12 @@ android:inputType="number" android:maxLength="6" android:textSize="20sp" - app:mnPsw_border_selected_color="#FF0000" app:mnPsw_border_color="#c9c9c9" + app:mnPsw_border_selected_color="#FF0000" app:mnPsw_border_width="2dp" app:mnPsw_item_margin="10dp" - app:mnPsw_mode="OriginalText" + app:mnPsw_mode="Text" + app:mnPsw_cover_text="码" app:mnPsw_style="StyleUnderLine" app:mnPsw_text_color="#393939" /> @@ -51,18 +54,17 @@ android:inputType="number" android:maxLength="5" android:textSize="20sp" - app:mnPsw_background_color="@color/colorAccent" - app:mnPsw_border_color="@color/colorPrimary" - app:mnPsw_border_radius="1dp" - app:mnPsw_border_width="8dp" - app:mnPsw_item_margin="15dp" + app:mnPsw_background_color="#cfcfcf" + app:mnPsw_border_color="#46c4b1" + app:mnPsw_border_radius="30dp" + app:mnPsw_border_width="1dp" + app:mnPsw_item_margin="20dp" app:mnPsw_mode="OriginalText" app:mnPsw_style="StyleOneself" app:mnPsw_text_color="#FFFFFF" /> + + diff --git a/pswedittextlibrary/src/main/java/com/maning/pswedittextlibrary/MNPasswordEditText.java b/pswedittextlibrary/src/main/java/com/maning/pswedittextlibrary/MNPasswordEditText.java index c2b585f..f7bc04d 100644 --- a/pswedittextlibrary/src/main/java/com/maning/pswedittextlibrary/MNPasswordEditText.java +++ b/pswedittextlibrary/src/main/java/com/maning/pswedittextlibrary/MNPasswordEditText.java @@ -21,38 +21,87 @@ import java.lang.reflect.Field; /** - * Created by maning on 2017/8/16. + * @author : maning + * @desc : 验证码和密码的输入框 */ - public class MNPasswordEditText extends EditText { private static final String TAG = "MNPasswordEditText"; private Context mContext; - //长度 + /** + * 长度 + */ private int maxLength; - //文字大小 - private float textSize; - //文字的画笔 + /** + * 文字的颜色 + */ + private int textColor; + /** + * 文字的画笔 + */ private Paint mPaintText; - //线的画笔 + /** + * 线框的画笔 + */ private Paint mPaintLine; - //背景色 + /** + * 背景色 + */ private int backgroundColor; + /** + * 线框的颜色 + */ private int borderColor; + /** + * 线框被选中的颜色 + */ private int borderSelectedColor; - private int textColor; + /** + * 线框的圆角 + */ private float borderRadius; + /** + * 线框的宽度 + */ private float borderWidth; + /** + * 密码框的间隔 + */ private float itemMargin; + /** + * 输入的类型 + */ private int inputMode; + /** + * 样式 + */ private int editTextStyle; - //文字遮盖 + /** + * 文字遮盖 + */ private String coverText; + /** + * 图片遮盖 + */ private int coverBitmapID; + /** + * 图片宽度 + */ + private float coverBitmapWidth; + /** + * 圆形遮盖的颜色 + */ private int coverCirclrColor; + /** + * 圆形遮盖的半径 + */ private float coverCirclrRadius; - private float coverBitmapWidth; + /** + * 线框背景 + */ + private GradientDrawable gradientDrawable = new GradientDrawable(); + private Bitmap coverBitmap; public MNPasswordEditText(Context context) { @@ -116,23 +165,6 @@ private void initAttrs(AttributeSet attrs, int defStyleAttr) { private void init() { //最大的长度 maxLength = getMaxLength(); - - //文字的大小 - textSize = getTextSize(); - - //初始化画笔 - //文字 - mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG); - mPaintText.setStyle(Paint.Style.FILL); - mPaintText.setColor(textColor); - mPaintText.setTextSize(textSize); - - //线 - mPaintLine = new Paint(Paint.ANTI_ALIAS_FLAG); - mPaintLine.setStyle(Paint.Style.STROKE); - mPaintLine.setColor(borderColor); - mPaintLine.setStrokeWidth(borderWidth); - //隐藏光标 setCursorVisible(false); //设置本来文字的颜色为透明 @@ -147,6 +179,29 @@ public boolean onLongClick(View view) { } }); + //初始化画笔 + //文字 + mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG); + mPaintText.setStyle(Paint.Style.FILL); + mPaintText.setColor(textColor); + mPaintText.setTextSize(getTextSize()); + + //线 + mPaintLine = new Paint(Paint.ANTI_ALIAS_FLAG); + mPaintLine.setStyle(Paint.Style.STROKE); + mPaintLine.setColor(borderColor); + mPaintLine.setStrokeWidth(borderWidth); + + //遮盖是图片方式,提前加载图片 + if (inputMode == 2) { + //判断有没有图片 + if (coverBitmapID == -1) { + //抛出异常 + throw new NullPointerException("遮盖图片为空"); + } else { + coverBitmap = BitmapFactory.decodeResource(getContext().getResources(), coverBitmapID); + } + } } @@ -160,17 +215,16 @@ protected void onDraw(Canvas canvas) { //判断类型 if (editTextStyle == 1) { - //初始化背景框 - GradientDrawable drawable = new GradientDrawable(); - drawable.setStroke((int) borderWidth, borderColor); - drawable.setCornerRadius(borderRadius); - drawable.setColor(backgroundColor); + //连体框 + gradientDrawable.setStroke((int) borderWidth, borderColor); + gradientDrawable.setCornerRadius(borderRadius); + gradientDrawable.setColor(backgroundColor); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { //Android系统大于等于API16,使用setBackground - setBackground(drawable); + setBackground(gradientDrawable); } else { //Android系统小于API16,使用setBackgroundDrawable - setBackgroundDrawable(drawable); + setBackgroundDrawable(gradientDrawable); } float itemW = measuredWidth / maxLength; //画线 @@ -182,17 +236,17 @@ protected void onDraw(Canvas canvas) { canvas.drawLine(startX, startY, stopX, stopY, mPaintLine); } } else if (editTextStyle == 2) { + //方形框 float margin = itemMargin; float itemW = measuredWidth / maxLength - margin; - GradientDrawable drawable = new GradientDrawable(); - drawable.setStroke((int) borderWidth, borderColor); - drawable.setCornerRadius(borderRadius); - drawable.setColor(backgroundColor); - Bitmap bitmap = drawableToBitmap(drawable, (int) itemW, (int) itemH); + gradientDrawable.setStroke((int) borderWidth, borderColor); + gradientDrawable.setCornerRadius(borderRadius); + gradientDrawable.setColor(backgroundColor); + Bitmap bitmap = drawableToBitmap(gradientDrawable, (int) itemW, (int) itemH); Bitmap bitmapSelected = null; if (borderSelectedColor != 0) { - drawable.setStroke((int) borderWidth, borderSelectedColor); - bitmapSelected = drawableToBitmap(drawable, (int) itemW, (int) itemH); + gradientDrawable.setStroke((int) borderWidth, borderSelectedColor); + bitmapSelected = drawableToBitmap(gradientDrawable, (int) itemW, (int) itemH); } //画每个Item背景 for (int i = 0; i < maxLength; i++) { @@ -253,23 +307,15 @@ protected void onDraw(Canvas canvas) { } float startX = (measuredWidth / maxLength - picW) / 2.0f + measuredWidth / maxLength * i; float startY = (itemH - picW) / 2.0f; - //判断有没有图片 - if (coverBitmapID == -1) { - //抛出异常 - throw new NullPointerException("遮盖图片为空"); - } else { - Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), coverBitmapID); - bitmap = Bitmap.createScaledBitmap(bitmap, (int) picW, (int) picW, true); - canvas.drawBitmap(bitmap, startX, startY, mPaintText); - } + Bitmap bitmap = Bitmap.createScaledBitmap(coverBitmap, (int) picW, (int) picW, true); + canvas.drawBitmap(bitmap, startX, startY, mPaintText); } else if (inputMode == 3) { - String StrPosition = coverText; - float fontWidth = getFontWidth(mPaintText, StrPosition); - float fontHeight = getFontHeight(mPaintText, StrPosition); + float fontWidth = getFontWidth(mPaintText, coverText); + float fontHeight = getFontHeight(mPaintText, coverText); float startX = (measuredWidth / maxLength - fontWidth) / 2.0f + measuredWidth / maxLength * i; float startY = (itemH + fontHeight) / 2.0f - 6; mPaintText.setColor(textColor); - canvas.drawText(StrPosition, startX, startY, mPaintText); + canvas.drawText(coverText, startX, startY, mPaintText); } else { String StrPosition = String.valueOf(currentText.charAt(i)); float fontWidth = getFontWidth(mPaintText, StrPosition); @@ -302,8 +348,12 @@ protected void onTextChanged(CharSequence text, int start, int lengthBefore, int super.onTextChanged(text, start, lengthBefore, lengthAfter); //刷新界面 invalidate(); - if (mOnPasswordChangeListener != null) { - mOnPasswordChangeListener.onPasswordChange(getText().toString()); + if (onTextChangeListener != null) { + if (getText().toString().length() == getMaxLength()) { + onTextChangeListener.onTextChange(getText().toString(), true); + } else { + onTextChangeListener.onTextChange(getText().toString(), false); + } } } @@ -346,14 +396,20 @@ private int dip2px(float dpValue) { return (int) (dpValue * scale + 0.5f); } - private OnPasswordChangeListener mOnPasswordChangeListener; + private OnTextChangeListener onTextChangeListener; - public void setOnPasswordChangeListener(OnPasswordChangeListener onPasswordChangeListener) { - mOnPasswordChangeListener = onPasswordChangeListener; + public void setOnTextChangeListener(OnTextChangeListener onTextChangeListener) { + this.onTextChangeListener = onTextChangeListener; } - public interface OnPasswordChangeListener { - void onPasswordChange(String password); + public interface OnTextChangeListener { + /** + * 监听输入变化 + * + * @param text 当前的文案 + * @param isComplete 是不是完成输入 + */ + void onTextChange(String text, boolean isComplete); } } diff --git a/pswedittextlibrary/src/main/res/values/attr.xml b/pswedittextlibrary/src/main/res/values/attr.xml index 2b531a7..8686ae7 100644 --- a/pswedittextlibrary/src/main/res/values/attr.xml +++ b/pswedittextlibrary/src/main/res/values/attr.xml @@ -2,7 +2,6 @@ -