Skip to content

Commit

Permalink
1、新增实心高亮模式,支持块状高亮和边界高亮
Browse files Browse the repository at this point in the history
2、支持错误高亮
  • Loading branch information
WGwangguan committed May 20, 2021
1 parent 8107fa3 commit 3852b86
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 16 deletions.
19 changes: 19 additions & 0 deletions app/src/main/java/com/kenny/separatededittextdemo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

edit_solid.setTextChangedListener(object : SeparatedEditText.TextChangedListener {
override fun textChanged(changeText: CharSequence?) {
}

override fun textCompleted(text: CharSequence?) {
edit_solid.showError()
}

})
edit_underline.setTextChangedListener(object : SeparatedEditText.TextChangedListener {
override fun textChanged(changeText: CharSequence?) {
}

override fun textCompleted(text: CharSequence?) {
edit_underline.showError()
}

})
}

fun handleContent(v: View?) {
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="number"
app:blockColor="@color/colorPrimary"
app:blockSpacing="2dp"
app:corner="10dp"
app:blockColor="#149B9EA3"
app:borderWidth="1dp"
app:blockSpacing="4dp"
app:corner="8dp"
app:cursorColor="@android:color/holo_green_light"
app:cursorDuration="1000"
app:cursorWidth="3dp"
app:highLightEnable="true"
app:highlightColor="@color/colorAccent"
app:maxLength="9"
app:highlightColor="#3E93B2"
app:maxLength="6"
app:separateType="@integer/type_solid"
app:showKeyboard="false"
app:textColor="@color/colorAccent" />
app:highlightStyle="@integer/style_border"
app:textColor="#D9000000" />

<com.kenny.separatededittext.SeparatedEditText
android:id="@+id/edit_underline"
Expand All @@ -40,10 +42,10 @@
app:borderColor="@color/lightGrey"
app:highLightBefore="true"
app:highLightEnable="true"
app:highlightColor="@color/colorAccent"
app:highlightColor="#D9000000"
app:separateType="@integer/type_underline"
app:showKeyboard="false"
app:textColor="@color/colorAccent" />
app:textColor="#D9000000" />


<com.kenny.separatededittext.SeparatedEditText
Expand All @@ -60,7 +62,6 @@
app:highlightColor="@color/colorAccent"
app:maxLength="4"
app:separateType="@integer/type_hollow"
app:showKeyboard="false"
app:textColor="@color/colorAccent" />


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.IntDef
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.content.ContextCompat
import java.util.*
Expand Down Expand Up @@ -56,13 +57,15 @@ class SeparatedEditText @JvmOverloads constructor(context: Context, attrs: Attri
private var type: Int//实心方式、空心方式

private var highLightEnable: Boolean // 是否显示框框高亮
private var highLightStyle: Int // 高亮样式,仅支持 solid

private var showKeyboard: Boolean

private var borderColor: Int
private var blockColor: Int
private var textColor: Int
private var highLightColor: Int // 框框高亮颜色
private var errorColor: Int // 框框错误颜色

private var highLightBefore = false // 待输入之前的一并高亮
private var isCursorShowing = false
Expand All @@ -71,6 +74,8 @@ class SeparatedEditText @JvmOverloads constructor(context: Context, attrs: Attri
private lateinit var timer: Timer
private lateinit var timerTask: TimerTask

private var showError = false

fun setSpacing(spacing: Int) {
this.spacing = spacing
postInvalidate()
Expand Down Expand Up @@ -123,7 +128,7 @@ class SeparatedEditText @JvmOverloads constructor(context: Context, attrs: Attri
postInvalidate()
}

fun setType(type: Int) {
fun setType(@TypeDef type: Int) {
this.type = type
postInvalidate()
}
Expand All @@ -148,17 +153,34 @@ class SeparatedEditText @JvmOverloads constructor(context: Context, attrs: Attri
postInvalidate()
}

fun setErrorColor(color: Int) {
this.errorColor = color
postInvalidate()
}

fun showError() {
if (this.type in listOf(TYPE_SOLID, TYPE_UNDERLINE)) {
showError = true
postInvalidate()
}
}

fun setHighlightStyle(@StyleDef style: Int) {
this.highLightStyle = style
postInvalidate()
}

private fun init() {
this.isFocusableInTouchMode = true
this.isFocusable = true
this.requestFocus()
this.isCursorVisible = false
this.filters = arrayOf<InputFilter>(LengthFilter(maxLength))
if (showKeyboard){
if (showKeyboard) {
Handler().postDelayed({
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED)
}, 500)
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED)
}, 500)
}

blockPaint = Paint().apply {
Expand Down Expand Up @@ -268,8 +290,37 @@ class SeparatedEditText @JvmOverloads constructor(context: Context, attrs: Attri
boxRectF[spacing * i + boxWidth * i.toFloat(), 0f, spacing * i + boxWidth * i + boxWidth.toFloat()] = boxHeight.toFloat()
val light = highLightBefore.matchValue(currentPos >= i, currentPos == i)
when (type) {
TYPE_SOLID -> canvas.drawRoundRect(boxRectF, corner.toFloat(), corner.toFloat(), blockPaint.apply { color = (highLightEnable && hasFocus() && light).matchValue(highLightColor, blockColor) })
TYPE_UNDERLINE -> canvas.drawLine(boxRectF.left, boxRectF.bottom, boxRectF.right, boxRectF.bottom, borderPaint.apply { color = (highLightEnable && hasFocus() && light).matchValue(highLightColor, borderColor) })
TYPE_SOLID -> {
if (showError) {
if (highLightStyle == STYLE_SOLID) {
canvas.drawRoundRect(boxRectF, corner.toFloat(), corner.toFloat(), blockPaint.apply { color = errorColor })
} else {
val tempRect = RectF(boxRectF.left + borderWidth / 2, boxRectF.top + borderWidth / 2, boxRectF.right - borderWidth / 2, boxRectF.bottom - borderWidth / 2)
canvas.drawRoundRect(tempRect, corner.toFloat(), corner.toFloat(), borderPaint.apply { color = errorColor })
}
continue@loop
}
if (highLightEnable && hasFocus() && light) {
if (highLightStyle == STYLE_SOLID) {
canvas.drawRoundRect(boxRectF, corner.toFloat(), corner.toFloat(), blockPaint.apply { color = highLightColor })
} else {
canvas.drawRoundRect(boxRectF, corner.toFloat(), corner.toFloat(), blockPaint.apply { color = blockColor })
val tempRect = RectF(boxRectF.left + borderWidth / 2, boxRectF.top + borderWidth / 2, boxRectF.right - borderWidth / 2, boxRectF.bottom - borderWidth / 2)
canvas.drawRoundRect(tempRect, corner.toFloat(), corner.toFloat(), borderPaint.apply { color = highLightColor })
}
} else {
canvas.drawRoundRect(boxRectF, corner.toFloat(), corner.toFloat(), blockPaint.apply { color = blockColor })
}
//
}
TYPE_UNDERLINE -> {
if (showError) {
canvas.drawLine(boxRectF.left, boxRectF.bottom, boxRectF.right, boxRectF.bottom, borderPaint.apply { color = errorColor })
continue@loop
}
canvas.drawLine(boxRectF.left, boxRectF.bottom, boxRectF.right, boxRectF.bottom, borderPaint.apply { color = (highLightEnable && hasFocus() && light).matchValue(highLightColor, borderColor) })

}
TYPE_HOLLOW -> {
if (i == 0 || i == maxLength) continue@loop
canvas.drawLine(boxRectF.left, boxRectF.top, boxRectF.left, boxRectF.bottom, borderPaint.apply { color = borderColor })
Expand All @@ -282,6 +333,7 @@ class SeparatedEditText @JvmOverloads constructor(context: Context, attrs: Attri

override fun onTextChanged(text: CharSequence, start: Int, lengthBefore: Int, lengthAfter: Int) {
super.onTextChanged(text, start, lengthBefore, lengthAfter)
showError = false
contentText = text
invalidate()
textChangedListener?.also {
Expand Down Expand Up @@ -360,6 +412,17 @@ class SeparatedEditText @JvmOverloads constructor(context: Context, attrs: Attri
private const val TYPE_HOLLOW = 1 //空心
private const val TYPE_SOLID = 2 //实心
private const val TYPE_UNDERLINE = 3 //下划线

private const val STYLE_SOLID = 1 //实心
private const val STYLE_BORDER = 2 // 边界

@Retention(AnnotationRetention.SOURCE)
@IntDef(STYLE_SOLID, STYLE_BORDER)
annotation class StyleDef

@Retention(AnnotationRetention.SOURCE)
@IntDef(TYPE_HOLLOW, TYPE_SOLID, TYPE_UNDERLINE)
annotation class TypeDef
}

init {
Expand Down Expand Up @@ -392,11 +455,13 @@ class SeparatedEditText @JvmOverloads constructor(context: Context, attrs: Attri
corner = ta.getDimension(R.styleable.SeparatedEditText_corner, 0f).toInt()
spacing = ta.getDimension(R.styleable.SeparatedEditText_blockSpacing, 0f).toInt()
type = ta.getInt(R.styleable.SeparatedEditText_separateType, TYPE_HOLLOW)
highLightStyle = ta.getInt(R.styleable.SeparatedEditText_highlightStyle, STYLE_SOLID)
maxLength = ta.getInt(R.styleable.SeparatedEditText_maxLength, 6)
cursorDuration = ta.getInt(R.styleable.SeparatedEditText_cursorDuration, 500)
cursorWidth = ta.getDimension(R.styleable.SeparatedEditText_cursorWidth, 2f).toInt()
borderWidth = ta.getDimension(R.styleable.SeparatedEditText_borderWidth, 5f).toInt()
showKeyboard = ta.getBoolean(R.styleable.SeparatedEditText_showKeyboard, true)
errorColor = ta.getColor(R.styleable.SeparatedEditText_errorColor, ContextCompat.getColor(getContext(), R.color.errorColor))
ta.recycle()
init()
}
Expand Down
5 changes: 5 additions & 0 deletions separatededittext/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@
<attr name="blockColor" format="color|reference" />
<attr name="textColor" format="color|reference" />
<attr name="highlightColor" format="color|reference" />
<attr name="highlightStyle" format="reference|integer" />
<attr name="blockSpacing" format="dimension" />
<attr name="cursorDuration" format="integer" />
<attr name="cursorWidth" format="dimension" />
<attr name="cursorColor" format="color|reference" />
<attr name="borderWidth" format="dimension" />
<attr name="showKeyboard" format="boolean" />
<attr name="errorColor" format="color|reference" />
</declare-styleable>

<integer name="type_hollow">1</integer>
<integer name="type_solid">2</integer>
<integer name="type_underline">3</integer>

<integer name="style_solid">1</integer>
<integer name="style_border">2</integer>

</resources>
1 change: 1 addition & 0 deletions separatededittext/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<color name="colorAccent">#FF4081</color>

<color name="lightGrey">#d3d3d3</color>
<color name="errorColor">#FF4D4F</color>
</resources>

0 comments on commit 3852b86

Please sign in to comment.