自动调整EditText的大小 [英] Autosizing EditText

查看:101
本文介绍了自动调整EditText的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android最近添加了对根据视图大小以及最小和最大文本大小调整TextViews文本大小大小的支持.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

Android recently added the support for resizing TextViews text size based on the view size and the min and max text size.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

不幸的是,它们不支持EditTexts,所以EditText还有其他替代方法吗?

Unfortunately, they don't support EditTexts, so Is there any other alternatives for EditText?

推荐答案

我被困住了,EditText是TextView的子级,但是不支持自动大小¿???

I was stuck as you, how EditText is child of TextView but don't support autosize ¿?¿?

我已经通过某种技巧实现了这一目标. 首先,我看到了TextView代码要复制并实现为EditTextView上的扩展(在Kotlin中),但是..有很多方法,所以最后我放弃了该选项.

I have achieve this with some kind of hack. First I saw the TextView code to copy and implement as extension (in Kotlin) on EditTextView, but.. there is a bulk of methods, so endly I discard that option.

我所做的是使用不可见的TextView(是的,我知道这是一个完整的技巧,对此不太满意,但Google应该为此感到羞耻)

What I have do, it's to use a TextView invisible (yes I know is a complete hack, am not very happy with that but Google should be ashamed about this)

这是我的xmls

    <TextView android:id="@+id/invisibleTextView"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:focusable="false"
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="@dimen/text_min"
    app:autoSizeMaxTextSize="@dimen/text_max"
    app:autoSizeStepGranularity="@dimen/text_step"
    android:textAlignment="center"
    app:layout_constraintLeft_toLeftOf="@id/main"
    app:layout_constraintRight_toRightOf="@id/main"
    app:layout_constraintTop_toBottomOf="@id/textCount"
    app:layout_constraintBottom_toBottomOf="@id/main"
    android:visibility="invisible"
    tool:text="This is a Resizable Textview" />


<EditText android:id="@+id/resizableEditText"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:textAlignment="center"
    app:layout_constraintLeft_toLeftOf="@id/main"
    app:layout_constraintRight_toRightOf="@id/main"
    app:layout_constraintTop_toBottomOf="@id/textCount"
    app:layout_constraintBottom_toBottomOf="@id/main"
    android:maxLength="@integer/max_text_length"
    tool:text="This is a Resizable EditTextView" />

注意:重要的是,两个视图的宽度/高度必须相同

Note: It's important both view have the same width/height

然后在我的代码中,我使用此textview的自动计算功能在EditTextView上使用.

Then on my code I use the autocalculations from this textview to use on my EditTextView.

private fun setupAutoresize() {
    invisibleTextView.setText("a", TextView.BufferType.EDITABLE) //calculate the right size for one character
    resizableEditText.textSize = autosizeText(invisibleTextView.textSize)
    resizableEditText.setHint(R.string.text_hint)

    resizableEditText.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(editable: Editable?) {
            resizableEditText.textSize = autosizeText(invisibleTextView.textSize)
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            textCount.text = currentCharacters.toString()
            val text = if (s?.isEmpty() ?: true) getString(R.string.text_hint) else s.toString()
            invisibleTextView.setText(text, TextView.BufferType.EDITABLE)
        }
    })
}

private fun autosizeText(size: Float): Float {
    return size / (resources.displayMetrics.density + MARGIN_FACTOR /*0.2f*/)
}

请注意,要更改提示的大小,请使用此 Android EditText提示大小.

As note, for change the size of hint i use this Android EditText Hint Size.

我知道这是一个艰苦的解决方法,但是至少我们可以确定,即使在将来的版本中进行可调整大小的更改,而专有或废弃的github库也会失败,这仍然可以继续工作.

I know it's a hard workaround, but at least we are sure this will continue working even when resizable change on future versions, while a propetary or abandoned github lib will fail.

我希望有一天,谷歌听到我们的声音,并在孩子身上实现这一奇妙的功能,并且我们可以避免所有这些东西

I hope some day, google hear us and implement on childs this wonderfull feature, and we could avoid all this stuff

希望这会有所帮助

这篇关于自动调整EditText的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆