久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

自動調整 EditText

Autosizing EditText(自動調整 EditText)
本文介紹了自動調整 EditText的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

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 ????

我通過某種 hack 實現了這一點.首先我看到了 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(是的,我知道這是一個完整的 hack,對此我不太滿意,但 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 日韩国产一区二区 | 国产中文字幕在线观看 | 这里只有精品在线观看 | 国产欧美精品一区二区色综合 | 欧美一级做性受免费大片免费 | 免费毛片视频 | 午夜视频网 | 性欧美69| 黄色网址免费看 | 丁香激情网 | 午夜视频在线播放 | av免费观看在线 | 天天综合天天 | 特级黄色大片 | 一区二区三区视频在线 | www亚洲| 欧美精品第一页 | 伊人国产在线 | 亚洲成人日韩 | 久久国 | 日韩超碰| 插少妇 | 欧美国产在线观看 | 亚洲国产成人精品女人久久久 | 97人人干 | 成人黄性视频 | 女人av在线 | 九九99久久 | 特黄aaaaaaaaa真人毛片 | 欧美激情亚洲 | 婷婷天堂 | 4hu在线| av三级在线观看 | 精品国产aⅴ麻豆 | 国产精品免费一区二区三区 | 在线黄色av| 久久精品视频网站 | 精品视频国产 | 精品欧美一区二区三区久久久 | 99久久久国产精品免费蜜臀 | 可以免费看av的网站 |