Skip to content

Commit

Permalink
回调监听修改
Browse files Browse the repository at this point in the history
  • Loading branch information
maning committed Jul 25, 2018
1 parent bd4d3fa commit 774b48a
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

Expand Down
39 changes: 30 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" />

Expand All @@ -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" />

<com.maning.pswedittextlibrary.MNPasswordEditText
android:layout_width="match_parent"
app:mnPsw_border_selected_color="#dcce10"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
Expand All @@ -71,7 +73,8 @@
android:textSize="20sp"
app:mnPsw_background_color="#FFFFFF"
app:mnPsw_border_color="#7e7e7e"
app:mnPsw_border_radius="4dp"
app:mnPsw_border_radius="10dp"
app:mnPsw_border_selected_color="#dcce10"
app:mnPsw_border_width="2dp"
app:mnPsw_cover_bitmap_id="@mipmap/ic_launcher"
app:mnPsw_cover_bitmap_width="30dp"
Expand All @@ -80,5 +83,23 @@
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#393939" />

<com.maning.pswedittextlibrary.MNPasswordEditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:inputType="number"
android:maxLength="6"
android:textSize="20sp"
app:mnPsw_border_color="#267eb4"
app:mnPsw_border_radius="4dp"
app:mnPsw_border_selected_color="#dc10a2"
app:mnPsw_border_width="2dp"
app:mnPsw_cover_text=""
app:mnPsw_item_margin="10dp"
app:mnPsw_mode="Text"
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#393939" />


</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
//设置本来文字的颜色为透明
Expand All @@ -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);
}
}
}


Expand All @@ -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;
//画线
Expand All @@ -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++) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
}

}
1 change: 0 additions & 1 deletion pswedittextlibrary/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<resources>

<declare-styleable name="MNPasswordEditText">

<!--密码框的颜色-->
<attr name="mnPsw_border_color" format="color" />
<!--密码框选中的颜色-->
Expand Down

0 comments on commit 774b48a

Please sign in to comment.