Skip to content

Commit e2b2b11

Browse files
committed
支持自定义样式
1 parent d012c35 commit e2b2b11

19 files changed

Lines changed: 642 additions & 290 deletions

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ password | 密码圆点展示
6161

6262
```
6363
-keep class com.lwjfork.code.** { *;}
64+
-dontwarn com.lwjfork.code.**
6465
```
6566

6667
## 效果图
@@ -71,3 +72,6 @@ password | 密码圆点展示
7172
## Change list
7273
##1.0.1
7374
1. 添加 delete 和 addChar 方法 方便自定义键盘
75+
76+
##1.0.2
77+
2. 支持自定义样式,具体参考demo里的 CustomStyleActivity

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<category android:name="android.intent.category.LAUNCHER" />
1717
</intent-filter>
1818
</activity>
19+
20+
21+
<activity android:name=".SimpleActivity" />
22+
<activity android:name=".CustomStyleActivity" />
1923
</application>
2024

2125
</manifest>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.lwjfork.example;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.util.Log;
8+
import android.view.View;
9+
10+
import com.lwjfork.code.CodeEditText;
11+
import com.lwjfork.example.style.CustomTextDrawer;
12+
import com.lwjfork.example.style.CustomeBlockDrawer;
13+
14+
/**
15+
* Created by lwj on 2019/1/17.
16+
* lwjfork@gmail.com
17+
*/
18+
public class CustomStyleActivity extends Activity {
19+
20+
21+
public static void launch(Context context) {
22+
Intent intent = new Intent();
23+
intent.setClass(context, CustomStyleActivity.class);
24+
context.startActivity(intent);
25+
}
26+
27+
28+
@Override
29+
protected void onCreate(Bundle savedInstanceState) {
30+
super.onCreate(savedInstanceState);
31+
setContentView(R.layout.activity_custom_style);
32+
final CodeEditText et_test = (CodeEditText) findViewById(R.id.et_test);
33+
34+
et_test.setOnTextChangedListener(new CodeEditText.OnTextChangedListener() {
35+
@Override
36+
public void onCodeChanged(CharSequence changeText) {
37+
Log.e(" onCodeChanged Text ", changeText + "");
38+
}
39+
40+
@Override
41+
public void onInputCompleted(CharSequence text) {
42+
Log.e("onInputCompleted Text ", text + "");
43+
}
44+
});
45+
findViewById(R.id.btn_chang_block).setOnClickListener(new View.OnClickListener() {
46+
@Override
47+
public void onClick(View v) {
48+
et_test.setBlockShape(new CustomeBlockDrawer());
49+
}
50+
});
51+
findViewById(R.id.btn_chang_text).setOnClickListener(new View.OnClickListener() {
52+
@Override
53+
public void onClick(View v) {
54+
et_test.setCodeInputType(new CustomTextDrawer());
55+
}
56+
});
57+
}
58+
}

app/src/main/java/com/lwjfork/example/MainActivity.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5-
import android.util.Log;
65
import android.view.View;
7-
import android.widget.Toast;
8-
9-
import com.lwjfork.code.CodeEditText;
106

117

128
public class MainActivity extends AppCompatActivity {
@@ -15,29 +11,17 @@ public class MainActivity extends AppCompatActivity {
1511
protected void onCreate(Bundle savedInstanceState) {
1612
super.onCreate(savedInstanceState);
1713
setContentView(R.layout.activity_main);
18-
final CodeEditText et_test = (CodeEditText) findViewById(R.id.et_test);
19-
20-
et_test.setOnTextChangedListener(new CodeEditText.OnTextChangedListener() {
21-
@Override
22-
public void onCodeChanged(CharSequence changeText) {
23-
Log.e("Text ", changeText + "");
24-
}
2514

26-
@Override
27-
public void onInputCompleted(CharSequence text) {
28-
Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
29-
}
30-
});
31-
findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
15+
findViewById(R.id.btn_simple).setOnClickListener(new View.OnClickListener() {
3216
@Override
3317
public void onClick(View v) {
34-
et_test.addChar('1');
18+
SimpleActivity.launch(MainActivity.this);
3519
}
3620
});
37-
findViewById(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
21+
findViewById(R.id.btn_custom_style).setOnClickListener(new View.OnClickListener() {
3822
@Override
3923
public void onClick(View v) {
40-
et_test.delete();
24+
CustomStyleActivity.launch(MainActivity.this);
4125
}
4226
});
4327
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.lwjfork.example;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.util.Log;
8+
9+
import com.lwjfork.code.CodeEditText;
10+
11+
/**
12+
* Created by lwj on 2019/1/17.
13+
* lwjfork@gmail.com
14+
*/
15+
public class SimpleActivity extends Activity {
16+
17+
18+
public static void launch(Context context) {
19+
Intent intent = new Intent();
20+
intent.setClass(context, SimpleActivity.class);
21+
context.startActivity(intent);
22+
}
23+
24+
@Override
25+
protected void onCreate(Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
setContentView(R.layout.activity_simple);
28+
29+
CodeEditText et_test = findViewById(R.id.et_test);
30+
et_test.setOnTextChangedListener(new CodeEditText.OnTextChangedListener() {
31+
@Override
32+
public void onCodeChanged(CharSequence changeText) {
33+
Log.e("SimpleActivity", String.format("onCodeChanged -- %s", changeText + ""));
34+
}
35+
36+
@Override
37+
public void onInputCompleted(CharSequence text) {
38+
Log.e("SimpleActivity", String.format("onInputCompleted -- %s", text + ""));
39+
}
40+
});
41+
}
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.lwjfork.example.style;
2+
3+
import android.graphics.Paint;
4+
import android.graphics.Rect;
5+
6+
import com.lwjfork.code.text.BaseTextDrawer;
7+
8+
/**
9+
* Created by lwj on 2019/1/17.
10+
* lwjfork@gmail.com
11+
*/
12+
public class CustomTextDrawer extends BaseTextDrawer {
13+
14+
private int textBaseLineY;
15+
16+
public CustomTextDrawer() {
17+
super();
18+
}
19+
20+
@Override
21+
protected void drawText(Rect rect, char c) {
22+
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
23+
float top = fontMetrics.top;// 基线到字体上边框的距离
24+
float bottom = fontMetrics.bottom;// 基线到字体下边框的距离
25+
textBaseLineY = (int) (rect.centerY() - top / 2 - bottom / 2);
26+
canvas.drawText("X", rect.centerX(), textBaseLineY, textPaint);
27+
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.lwjfork.example.style;
2+
3+
import android.graphics.Color;
4+
import android.graphics.Paint;
5+
import android.graphics.RectF;
6+
7+
import com.lwjfork.code.block.BaseBlockDrawer;
8+
9+
/**
10+
* Created by lwj on 2019/1/17.
11+
* lwjfork@gmail.com
12+
*/
13+
public class CustomeBlockDrawer extends BaseBlockDrawer {
14+
15+
16+
public CustomeBlockDrawer() {
17+
super();
18+
}
19+
20+
@Override
21+
protected void initPaint() {
22+
super.initPaint();
23+
blockPaint.setStyle(Paint.Style.FILL_AND_STROKE);
24+
}
25+
26+
27+
@Override
28+
protected void drawFocusedBlock(RectF rectF) {
29+
drawRect(Color.BLUE, rectF);
30+
}
31+
32+
@Override
33+
protected void drawNormalBlock(RectF rectF) {
34+
drawRect(Color.GREEN, rectF);
35+
}
36+
37+
@Override
38+
protected void drawErrorBlock(RectF rectF) {
39+
drawRect(Color.RED, rectF);
40+
}
41+
42+
private void drawRect(int color, RectF rectF) {
43+
if (rectF == null) {
44+
return;
45+
}
46+
blockPaint.setColor(color);
47+
canvas.drawRoundRect(rectF, blockCorner, blockCorner, blockPaint);
48+
}
49+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
tools:context="com.lwjfork.example.MainActivity">
9+
10+
<LinearLayout
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:orientation="vertical">
14+
15+
16+
<com.lwjfork.code.CodeEditText
17+
android:id="@+id/et_test"
18+
android:layout_width="match_parent"
19+
android:layout_height="50dp"
20+
android:layout_margin="10dp"
21+
android:layout_marginTop="20dp"
22+
android:focusable="true"
23+
android:focusableInTouchMode="true"
24+
android:inputType="number"
25+
android:maxLength="6"
26+
android:paddingLeft="10dp"
27+
android:paddingRight="10dp"
28+
app:blockErrorColor="@android:color/holo_red_light"
29+
app:blockFocusColor="@android:color/holo_blue_bright"
30+
app:blockLineWidth="1dp"
31+
app:blockNormalColor="@android:color/black"
32+
app:blockShape="underline"
33+
app:blockSpace="15dp"
34+
app:codeInputType="text"
35+
app:codeTextColor="@android:color/black"
36+
app:codeTextSize="30sp"
37+
app:cursorWidth="4dp"
38+
app:maxCodeLength="6"
39+
app:showCursor="true"
40+
tools:text="111" />
41+
42+
43+
<LinearLayout
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:layout_marginTop="20dp"
47+
android:orientation="vertical">
48+
49+
<Button
50+
android:id="@+id/btn_chang_block"
51+
android:layout_width="match_parent"
52+
android:layout_height="50dp"
53+
android:layout_marginTop="20dp"
54+
android:text="修改背景" />
55+
56+
<Button
57+
android:id="@+id/btn_chang_text"
58+
android:layout_width="match_parent"
59+
android:layout_height="50dp"
60+
android:layout_marginTop="20dp"
61+
android:text="修改文本展示" />
62+
</LinearLayout>
63+
64+
65+
</LinearLayout>
66+
67+
68+
</ScrollView>

0 commit comments

Comments
 (0)