Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.37 KB | None | 0 0
  1. package com.sudox.design.views
  2.  
  3. import android.annotation.SuppressLint
  4. import android.content.Context
  5. import android.text.InputType
  6. import android.util.AttributeSet
  7. import android.view.Gravity
  8. import android.view.ViewGroup
  9. import android.widget.EditText
  10. import androidx.appcompat.widget.AppCompatEditText
  11. import androidx.core.content.res.getDimensionPixelSizeOrThrow
  12. import androidx.core.content.res.getIntOrThrow
  13. import androidx.core.content.res.use
  14. import com.sudox.design.R
  15. import kotlin.math.min
  16.  
  17. class CodeEditText : ViewGroup {
  18.  
  19.     private var digitEditTexts: Array<EditText>? = null
  20.     private var digitsMargin = 0
  21.     private var digitWidth = 0
  22.  
  23.     constructor(context: Context) : this(context, null)
  24.     constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, R.attr.codeEditTextStyle)
  25.  
  26.     @SuppressLint("Recycle")
  27.     constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
  28.         context
  29.                 .obtainStyledAttributes(attrs, R.styleable.CodeEditText, defStyleAttr, R.style.Theme_Sudox_CodeEditText)
  30.                 .use {
  31.                     digitsMargin = it.getDimensionPixelSizeOrThrow(R.styleable.CodeEditText_digitsMargin)
  32.                     digitWidth = it.getDimensionPixelSizeOrThrow(R.styleable.CodeEditText_digitWidth)
  33.  
  34.                     val digitsCount = it.getIntOrThrow(R.styleable.CodeEditText_digitsCount)
  35.  
  36.                     digitEditTexts = Array(digitsCount) {
  37.                         AppCompatEditText(context).apply {
  38.                             minWidth = digitWidth
  39.                             maxWidth = digitWidth
  40.  
  41.                             gravity = Gravity.CENTER_HORIZONTAL
  42.                             inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL
  43.                             isSingleLine = true
  44.                             maxLines = 1
  45.  
  46.                             addView(this, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
  47.                         }
  48.                     }
  49.                 }
  50.     }
  51.  
  52.     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
  53.         val widthSize = MeasureSpec.getSize(widthMeasureSpec)
  54.         val widthMode = MeasureSpec.getMode(widthMeasureSpec)
  55.         val digitsWidth = digitEditTexts!!.sumBy {
  56.             measureChild(it, widthMeasureSpec, heightMeasureSpec)
  57.             it.measuredWidth
  58.         }
  59.  
  60.         val needHeight = digitEditTexts!![0].measuredHeight + paddingTop + paddingBottom
  61.         val needWidth = digitsWidth + digitsMargin * (digitEditTexts!!.size - 1) + paddingRight + paddingLeft
  62.         val width = if (widthMode == MeasureSpec.EXACTLY) {
  63.             widthSize
  64.         } else if (widthMode == MeasureSpec.AT_MOST) {
  65.             min(widthSize, needWidth)
  66.         } else {
  67.             needWidth
  68.         }
  69.  
  70.         setMeasuredDimension(width, needHeight)
  71.     }
  72.  
  73.     override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
  74.         val topBorder = paddingTop
  75.         val bottomBorder = topBorder + digitEditTexts!![0].measuredHeight
  76.         var leftBorder = paddingLeft
  77.  
  78.         digitEditTexts!!.forEach {
  79.             val rightBorder = leftBorder + it.measuredWidth
  80.             it.layout(leftBorder, topBorder, rightBorder, bottomBorder)
  81.             leftBorder = rightBorder + digitsMargin
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement