How to hide underbar in EditText

后端 未结 25 1769
温柔的废话
温柔的废话 2020-11-28 00:52

How can I hide the EditText underbar (the prompt line with little serifs at the ends)?

There might be a better way to do what I want: I have a layout with an EditTe

相关标签:
25条回答
  • 2020-11-28 01:33

    Here's a way to hide it, without ruining the default padding:

    fun View.setViewBackgroundWithoutResettingPadding(background: Drawable?) {
        val paddingBottom = this.paddingBottom
        val paddingStart = ViewCompat.getPaddingStart(this)
        val paddingEnd = ViewCompat.getPaddingEnd(this)
        val paddingTop = this.paddingTop
        ViewCompat.setBackground(this, background)
        ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
    }
    

    usage:

    editText.setViewBackgroundWithoutResettingPadding(null)
    

    Update:

    If you find yourself always passing null, you can codify that in the method (and then you might as well overload EditText itself)

    fun EditText.removeUnderline() {
        val paddingBottom = this.paddingBottom
        val paddingStart = ViewCompat.getPaddingStart(this)
        val paddingEnd = ViewCompat.getPaddingEnd(this)
        val paddingTop = this.paddingTop
        ViewCompat.setBackground(this, null)
        ViewCompat.setPaddingRelative(this, paddingStart, paddingTop, paddingEnd, paddingBottom)
    }
    
    // usage:
    editText.removeUnderline()
    
    0 讨论(0)
提交回复
热议问题