Skip to content

Commit

Permalink
添加下划线模式,配置选中颜色框
Browse files Browse the repository at this point in the history
  • Loading branch information
maning committed Jul 24, 2018
1 parent ca57d37 commit bd4d3fa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
29 changes: 7 additions & 22 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
<com.maning.pswedittextlibrary.MNPasswordEditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:inputType="number"
android:maxLength="6"
android:textSize="20sp"
app:mnPsw_border_color="#7e7e7e"
app:mnPsw_border_width="4dp"
app:mnPsw_border_selected_color="#FF0000"
app:mnPsw_border_color="#c9c9c9"
app:mnPsw_border_width="2dp"
app:mnPsw_item_margin="10dp"
app:mnPsw_mode="OriginalText"
app:mnPsw_style="StyleUnderLine"
Expand All @@ -52,32 +54,15 @@
app:mnPsw_background_color="@color/colorAccent"
app:mnPsw_border_color="@color/colorPrimary"
app:mnPsw_border_radius="1dp"
app:mnPsw_border_width="4dp"
app:mnPsw_border_width="8dp"
app:mnPsw_item_margin="15dp"
app:mnPsw_mode="OriginalText"
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#FFFFFF" />

<com.maning.pswedittextlibrary.MNPasswordEditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:inputType="number"
android:maxLength="4"
android:textSize="20sp"
app:mnPsw_background_color="#01e2c4"
app:mnPsw_border_color="#da7202"
app:mnPsw_border_radius="10dp"
app:mnPsw_border_width="10dp"
app:mnPsw_cover_text=""
app:mnPsw_item_margin="15dp"
app:mnPsw_mode="Text"
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#2e2e2e" />

<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 @@ -86,7 +71,7 @@
android:textSize="20sp"
app:mnPsw_background_color="#FFFFFF"
app:mnPsw_border_color="#7e7e7e"
app:mnPsw_border_radius="2dp"
app:mnPsw_border_radius="4dp"
app:mnPsw_border_width="2dp"
app:mnPsw_cover_bitmap_id="@mipmap/ic_launcher"
app:mnPsw_cover_bitmap_width="30dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void initAttrs(AttributeSet attrs, int defStyleAttr) {
//边框颜色
borderColor = array.getColor(R.styleable.MNPasswordEditText_mnPsw_border_color, Color.parseColor("#FF0000"));
//边框选中的颜色
borderSelectedColor = array.getColor(R.styleable.MNPasswordEditText_mnPsw_border_selected_color, Color.parseColor("#FF0000"));
borderSelectedColor = array.getColor(R.styleable.MNPasswordEditText_mnPsw_border_selected_color, 0);
//文字的颜色
textColor = array.getColor(R.styleable.MNPasswordEditText_mnPsw_text_color, Color.parseColor("#FF0000"));
//边框圆角
Expand Down Expand Up @@ -189,32 +189,42 @@ protected void onDraw(Canvas canvas) {
drawable.setCornerRadius(borderRadius);
drawable.setColor(backgroundColor);
Bitmap bitmap = drawableToBitmap(drawable, (int) itemW, (int) itemH);
drawable.setStroke((int) borderWidth, borderSelectedColor);
Bitmap bitmapSelected = drawableToBitmap(drawable, (int) itemW, (int) itemH);
Bitmap bitmapSelected = null;
if (borderSelectedColor != 0) {
drawable.setStroke((int) borderWidth, borderSelectedColor);
bitmapSelected = drawableToBitmap(drawable, (int) itemW, (int) itemH);
}
//画每个Item背景
for (int i = 0; i < maxLength; i++) {
float left = itemW * i + margin / 2 + margin * i;
float top = 0;
if (getText().length() == i) {
//选中是另外的颜色
canvas.drawBitmap(bitmapSelected, left, top, mPaintLine);
} else {
if (bitmapSelected == null) {
canvas.drawBitmap(bitmap, left, top, mPaintLine);
} else {
if (getText().length() == i) {
//选中是另外的颜色
canvas.drawBitmap(bitmapSelected, left, top, mPaintLine);
} else {
canvas.drawBitmap(bitmap, left, top, mPaintLine);
}
}

}
} else if (editTextStyle == 3) {
float itemW = (measuredWidth - itemMargin * (maxLength - 1) - itemMargin) / maxLength;
//下划线格式
for (int i = 0; i < maxLength; i++) {
if (getText().length() == i) {
//选中是另外的颜色
mPaintLine.setColor(borderSelectedColor);
if (borderSelectedColor != 0) {
if (getText().length() == i) {
//选中是另外的颜色
mPaintLine.setColor(borderSelectedColor);
} else {
mPaintLine.setColor(borderColor);
}
} else {
mPaintLine.setColor(borderColor);
}
float startX = itemW * i + itemMargin * i + itemMargin / 2;
float startY = itemH;
float startY = itemH - borderWidth;
float stopX = startX + itemW;
float stopY = startY;
canvas.drawLine(startX, startY, stopX, stopY, mPaintLine);
Expand Down
2 changes: 1 addition & 1 deletion pswedittextlibrary/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<!--密码框的颜色-->
<attr name="mnPsw_border_color" format="color" />
<!--选中的颜色-->
<!--密码框选中的颜色-->
<attr name="mnPsw_border_selected_color" format="color" />
<!--密码文字的颜色,圆形密码颜色-->
<attr name="mnPsw_text_color" format="color" />
Expand Down

0 comments on commit bd4d3fa

Please sign in to comment.