Android EditText Hint Size

前端 未结 12 1157
轮回少年
轮回少年 2020-11-28 06:21

How to reduce EditText Hint size?

12条回答
  •  攒了一身酷
    2020-11-28 06:54

    Using onFocusChanged() listener for changing hint font size is also an option, as addTextChangeListener() won't trigger when user clicks on text field, and blinking cursor will resize to hint font.

    Also, unlike with TextChangeListener, there is no need to set initial hint font size separately.

    class EditTextWithHintSize {
     init {
            val typedArray = context.obtainStyledAttributes(attrs,
                    R.styleable.EditTextWithHintSize, 0, defStyle)
            try {
                hintFontSize = typedArray.getDimension(R.styleable.EditTextWithHintSize_hint_font_size, textSize)
                fontSize = textSize
    
                if (length() == 0) {
                    setTextSize(TypedValue.COMPLEX_UNIT_PX, hintFontSize)
                }
            } catch (e: Exception) {
                hintFontSize = textSize
                fontSize = textSize
            } finally {
                typedArray.recycle()
            }
        }
    
        override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect)
    
            if (focused) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
            } else {
                if (length() == 0) {
                    setTextSize(TypedValue.COMPLEX_UNIT_PX, hintFontSize)
                } else {
                    setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
                }
            }
        }
    }
    

提交回复
热议问题