Заменить текст с плавающей подсказкой на заглавную - PullRequest
0 голосов
/ 11 июня 2019

Я просто хочу изменить подсказку на заглавную букву программно, когда EditText имеет фокус, как показано на рисунке ниже.

EditText when no focus

EditText when has focus

Я легко могу сделать это, следуя коду

var hintText:String = tip.hint.toString()

    editText.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
        if (hasFocus) {
            textInputLayout.hint = hintText.toUpperCase()
        } else {
            textInputLayout.hint = hintText
        }
    }

Здесь я храню подсказку в одной простой строковой переменной и устанавливаю подсказку в верхнем регистре, в то время как EditText имеет фокус

Но я не могусделай это с кастомами EditText

Вот мой CustomEditText класс:

 class CustomEditText : TextInputEditText {

    var isFilled: Boolean = false
    lateinit var contextt: Context

    constructor(context: Context) : super(context) {
        contextt = context
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        contextt = context
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int)
            : super(context, attrs, defStyle) {
        contextt = context
        init()
    }

    private fun init() {

    }

    override fun onTextChanged(text: CharSequence?, start: Int, lengthBefore: Int, lengthAfter: Int) {
        isFilled = text.toString().isNotEmpty()
        var context = context
        while (context is ContextWrapper) {
            if (context is Activity) {
                (context as BaseAct).etFillEvent()
            }
            context = context.baseContext
        }
    }

    override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect)

        val resColor: Int = if (focused)
            R.color.colorPrimary
        else
            R.color.grey
        this.background.setColorFilter(ContextCompat.getColor(context, resColor), PorterDuff.Mode.SRC_IN)
    }

    fun setError() {
        this.background.setColorFilter(
            ContextCompat.getColor(context, R.color.red),
            PorterDuff.Mode.SRC_IN
        )
        this.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0)
    }

    fun getString(): String {
        return this.text.toString().trim()
    }
}

Было бы здорово, если кто-нибудь может мне помочь

...