将浮动提示文本更改为大写字母

时间:2019-06-11 05:32:54

标签: android android-edittext textinputlayout

我只想以编程方式将提示更改为大写字母,例如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()
    }
}

如果有人可以帮助我,那就太好了

0 个答案:

没有答案