解决自定义MaterialEditText的问题

时间:2019-03-02 15:26:06

标签: android kotlin android-edittext material

我在自定义MaterialEditText上出现视觉异常。我修改了onDraw以显示字段验证器(是否有效的电子邮件地址)。您是否有解决此问题的途径?

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    conditions.forEach {
        val predicate = it.predicate(text.toString())
        val pen = when {
            showErrorIfNeeded && !predicate && !text.isNullOrEmpty() -> redPen
            predicate && !text.isNullOrEmpty() -> greenPen
            else -> greyPen
        }
        val icon = when {
            showErrorIfNeeded && !predicate && !text.isNullOrEmpty() -> errorIcon
            predicate && !text.isNullOrEmpty() -> okIcon
            else -> nokIcon
        }
        val label = if (showErrorIfNeeded && !predicate) it.errorLabel else it.normalLabel

        canvas.withTranslation(x = scrollX.toFloat(), y = scrollY.toFloat()) {
            drawText(label,
                    it.position.left + conditionIconHorizontalSpace,
                    it.position.top + (height - saveViewHeightExtra) + conditionLineHeight + conditionMargin,
                    pen)
            drawBitmap(icon,
                    it.position.left,
                    it.position.bottom + (height - saveViewHeightExtra) - icon.height + conditionMargin,
                    bitmapPaint)
        }
    }
}

enter image description here

0 个答案:

没有答案