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 e61643c commit ca57d37
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 52 deletions.
15 changes: 1 addition & 14 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 25 additions & 17 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dadada"
android:orientation="vertical"
android:padding="10dp"
>
android:orientation="vertical">

<TextView
android:id="@+id/tvShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="展现密码的地方"
/>
android:text="展现密码的地方" />


<com.maning.pswedittextlibrary.MNPasswordEditText
Expand All @@ -28,8 +24,22 @@
android:maxLength="6"
android:textSize="20sp"
app:mnPsw_cover_circle_color="@color/colorPrimaryDark"
app:mnPsw_cover_circle_radius="12dp"
/>
app:mnPsw_cover_circle_radius="12dp" />

<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="#7e7e7e"
app:mnPsw_border_width="4dp"
app:mnPsw_item_margin="10dp"
app:mnPsw_mode="OriginalText"
app:mnPsw_style="StyleUnderLine"
app:mnPsw_text_color="#393939" />

<com.maning.pswedittextlibrary.MNPasswordEditText
android:layout_width="match_parent"
Expand All @@ -46,8 +56,7 @@
app:mnPsw_item_margin="15dp"
app:mnPsw_mode="OriginalText"
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#FFFFFF"
/>
app:mnPsw_text_color="#FFFFFF" />

<com.maning.pswedittextlibrary.MNPasswordEditText
android:layout_width="match_parent"
Expand All @@ -65,27 +74,26 @@
app:mnPsw_item_margin="15dp"
app:mnPsw_mode="Text"
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#2e2e2e"
/>
app:mnPsw_text_color="#2e2e2e" />

<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:maxLength="4"
android:textSize="20sp"
app:mnPsw_background_color="#FFFFFF"
app:mnPsw_border_color="#7e7e7e"
app:mnPsw_border_radius="2dp"
app:mnPsw_border_width="2dp"
app:mnPsw_cover_bitmap_id="@mipmap/ic_launcher"
app:mnPsw_cover_bitmap_width="30dp"
app:mnPsw_item_margin="5dp"
app:mnPsw_item_margin="40dp"
app:mnPsw_mode="Bitmap"
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#393939"
/>
app:mnPsw_text_color="#393939" />


</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class MNPasswordEditText extends EditText {
//背景色
private int backgroundColor;
private int borderColor;
private int borderSelectedColor;
private int textColor;
private float borderRadius;
private float borderWidth;
Expand Down Expand Up @@ -80,6 +81,8 @@ private void initAttrs(AttributeSet attrs, int defStyleAttr) {
backgroundColor = array.getColor(R.styleable.MNPasswordEditText_mnPsw_background_color, Color.parseColor("#FFFFFF"));
//边框颜色
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"));
//文字的颜色
textColor = array.getColor(R.styleable.MNPasswordEditText_mnPsw_text_color, Color.parseColor("#FF0000"));
//边框圆角
Expand Down Expand Up @@ -178,21 +181,44 @@ protected void onDraw(Canvas canvas) {
float stopY = itemH;
canvas.drawLine(startX, startY, stopX, stopY, mPaintLine);
}
} else {
} 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);
drawable.setStroke((int) borderWidth, borderSelectedColor);
Bitmap 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;
canvas.drawBitmap(bitmap, left, top, mPaintLine);
}
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);
} else {
mPaintLine.setColor(borderColor);
}
float startX = itemW * i + itemMargin * i + itemMargin / 2;
float startY = itemH;
float stopX = startX + itemW;
float stopY = startY;
canvas.drawLine(startX, startY, stopX, stopY, mPaintLine);
}
}

//写文字
Expand Down
40 changes: 22 additions & 18 deletions pswedittextlibrary/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,49 @@
<declare-styleable name="MNPasswordEditText">

<!--密码框的颜色-->
<attr name="mnPsw_border_color" format="color"/>
<attr name="mnPsw_border_color" format="color" />
<!--选中的颜色-->
<attr name="mnPsw_border_selected_color" format="color" />
<!--密码文字的颜色,圆形密码颜色-->
<attr name="mnPsw_text_color" format="color"/>
<attr name="mnPsw_text_color" format="color" />
<!--密码框的圆角-->
<attr name="mnPsw_border_radius" format="dimension"/>
<attr name="mnPsw_border_radius" format="dimension" />
<!--密码框的线的大小-->
<attr name="mnPsw_border_width" format="dimension"/>
<attr name="mnPsw_border_width" format="dimension" />
<!--密码框的每个间隔,只有样式2才起作用-->
<attr name="mnPsw_item_margin" format="dimension"/>
<attr name="mnPsw_item_margin" format="dimension" />
<!--密码框背景色-->
<attr name="mnPsw_background_color" format="color"/>
<attr name="mnPsw_background_color" format="color" />
<!--密码框输入的模式:4.明文,3.文字,2.图片,1.圆形-->
<attr name="mnPsw_mode" format="enum">
<!--圆形默认-->
<enum name="Circle" value="1"/>
<enum name="Circle" value="1" />
<!--图片-->
<enum name="Bitmap" value="2"/>
<enum name="Bitmap" value="2" />
<!--文本-->
<enum name="Text" value="3"/>
<enum name="Text" value="3" />
<!--原始-->
<enum name="OriginalText" value="4"/>
<enum name="OriginalText" value="4" />
</attr>
<!--密码框样式: 1.连在一起 2.分开单独显示-->
<!--密码框样式: 1.连在一起 2.分开单独显示 3.下划线形式-->
<attr name="mnPsw_style" format="enum">
<!--连在一起-->
<enum name="StyleDefault" value="1"/>
<enum name="StyleDefault" value="1" />
<!--单独-->
<enum name="StyleOneself" value="2"/>
<enum name="StyleOneself" value="2" />
<!--下划线形式-->
<enum name="StyleUnderLine" value="3" />
</attr>
<!--密码文字遮盖-->
<attr name="mnPsw_cover_text" format="string"/>
<attr name="mnPsw_cover_text" format="string" />
<!--密码图片遮盖-->
<attr name="mnPsw_cover_bitmap_id" format="reference"/>
<attr name="mnPsw_cover_bitmap_id" format="reference" />
<!--密码圆形遮盖颜色-->
<attr name="mnPsw_cover_circle_color" format="color"/>
<attr name="mnPsw_cover_circle_color" format="color" />
<!--密码圆形遮盖半径-->
<attr name="mnPsw_cover_circle_radius" format="dimension"/>
<attr name="mnPsw_cover_circle_radius" format="dimension" />
<!--密码图片遮盖长宽-->
<attr name="mnPsw_cover_bitmap_width" format="dimension"/>
<attr name="mnPsw_cover_bitmap_width" format="dimension" />

</declare-styleable>

Expand Down

0 comments on commit ca57d37

Please sign in to comment.